PageRenderTime 44ms CodeModel.GetById 19ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 0ms

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