/src/core/Logging.cpp

http://github.com/imageworks/OpenColorIO · C++ · 156 lines · 96 code · 28 blank · 32 comment · 11 complexity · 6e38e2e88e55169c43165a431bebd7dc MD5 · raw file

  1. /*
  2. Copyright (c) 2003-2010 Sony Pictures Imageworks Inc., et al.
  3. All Rights Reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. * Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. * Neither the name of Sony Pictures Imageworks nor the names of its
  13. contributors may be used to endorse or promote products derived from
  14. this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  19. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <cstdlib>
  28. #include <iostream>
  29. #include <sstream>
  30. #include <OpenColorIO/OpenColorIO.h>
  31. #include "Logging.h"
  32. #include "Mutex.h"
  33. #include "pystring/pystring.h"
  34. OCIO_NAMESPACE_ENTER
  35. {
  36. namespace
  37. {
  38. const char * OCIO_LOGGING_LEVEL_ENVVAR = "OCIO_LOGGING_LEVEL";
  39. const LoggingLevel OCIO_DEFAULT_LOGGING_LEVEL = LOGGING_LEVEL_INFO;
  40. Mutex g_logmutex;
  41. LoggingLevel g_logginglevel = LOGGING_LEVEL_UNKNOWN;
  42. bool g_initialized = false;
  43. bool g_loggingOverride = false;
  44. // You must manually acquire the logging mutex before calling this.
  45. // This will set g_logginglevel, g_initialized, g_loggingOverride
  46. void InitLogging()
  47. {
  48. if(g_initialized) return;
  49. g_initialized = true;
  50. char* levelstr = std::getenv(OCIO_LOGGING_LEVEL_ENVVAR);
  51. if(levelstr)
  52. {
  53. g_loggingOverride = true;
  54. g_logginglevel = LoggingLevelFromString(levelstr);
  55. if(g_logginglevel == LOGGING_LEVEL_UNKNOWN)
  56. {
  57. std::cerr << "[OpenColorIO Warning]: Invalid $OCIO_LOGGING_LEVEL specified. ";
  58. std::cerr << "Options: none (0), warning (1), info (2), debug (3)" << std::endl;
  59. g_logginglevel = OCIO_DEFAULT_LOGGING_LEVEL;
  60. }
  61. }
  62. else
  63. {
  64. g_logginglevel = OCIO_DEFAULT_LOGGING_LEVEL;
  65. }
  66. }
  67. }
  68. LoggingLevel GetLoggingLevel()
  69. {
  70. AutoMutex lock(g_logmutex);
  71. InitLogging();
  72. return g_logginglevel;
  73. }
  74. void SetLoggingLevel(LoggingLevel level)
  75. {
  76. AutoMutex lock(g_logmutex);
  77. InitLogging();
  78. // Calls to SetLoggingLevel are ignored if OCIO_LOGGING_LEVEL_ENVVAR
  79. // is specified. This is to allow users to optionally debug OCIO at
  80. // runtime even in applications that disable logging.
  81. if(!g_loggingOverride)
  82. {
  83. g_logginglevel = level;
  84. }
  85. }
  86. void LogWarning(const std::string & text)
  87. {
  88. AutoMutex lock(g_logmutex);
  89. InitLogging();
  90. if(g_logginglevel<LOGGING_LEVEL_WARNING) return;
  91. std::vector<std::string> parts;
  92. pystring::split( pystring::rstrip(text), parts, "\n");
  93. for(unsigned int i=0; i<parts.size(); ++i)
  94. {
  95. std::cerr << "[OpenColorIO Warning]: " << parts[i] << std::endl;
  96. }
  97. }
  98. void LogInfo(const std::string & text)
  99. {
  100. AutoMutex lock(g_logmutex);
  101. InitLogging();
  102. if(g_logginglevel<LOGGING_LEVEL_INFO) return;
  103. std::vector<std::string> parts;
  104. pystring::split( pystring::rstrip(text), parts, "\n");
  105. for(unsigned int i=0; i<parts.size(); ++i)
  106. {
  107. std::cerr << "[OpenColorIO Info]: " << parts[i] << std::endl;
  108. }
  109. }
  110. void LogDebug(const std::string & text)
  111. {
  112. AutoMutex lock(g_logmutex);
  113. InitLogging();
  114. if(g_logginglevel<LOGGING_LEVEL_DEBUG) return;
  115. std::vector<std::string> parts;
  116. pystring::split( pystring::rstrip(text), parts, "\n");
  117. for(unsigned int i=0; i<parts.size(); ++i)
  118. {
  119. std::cerr << "[OpenColorIO Debug]: " << parts[i] << std::endl;
  120. }
  121. }
  122. bool IsDebugLoggingEnabled()
  123. {
  124. return (GetLoggingLevel()>=LOGGING_LEVEL_DEBUG);
  125. }
  126. }
  127. OCIO_NAMESPACE_EXIT