Changeset 2296


Ignore:
Timestamp:
26/09/2008 21:22:26 (3 years ago)
Author:
chris
Message:

Add file logger class.

Location:
box/trunk/lib/common
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • box/trunk/lib/common/Logging.cpp

    r2279 r2296  
    223223} 
    224224 
     225Logger::Logger(Log::Level Level)  
     226: mCurrentLevel(Level)  
     227{ 
     228        Logging::Add(this); 
     229} 
     230 
    225231Logger::~Logger()  
    226232{ 
     
    401407        ::openlog(mName.c_str(), LOG_PID, LOG_LOCAL6); 
    402408} 
     409 
     410bool FileLogger::Log(Log::Level Level, const std::string& rFile,  
     411        int line, std::string& rMessage) 
     412{ 
     413        if (Level > GetLevel()) 
     414        { 
     415                return true; 
     416        } 
     417         
     418        /* avoid infinite loop if this throws an exception */ 
     419        Logging::Remove(this); 
     420 
     421        std::ostringstream buf; 
     422        buf << FormatTime(GetCurrentBoxTime(), false); 
     423        buf << " "; 
     424 
     425        if (Level <= Log::FATAL) 
     426        { 
     427                buf << "[FATAL]   "; 
     428        } 
     429        else if (Level <= Log::ERROR) 
     430        { 
     431                buf << "[ERROR]   "; 
     432        } 
     433        else if (Level <= Log::WARNING) 
     434        { 
     435                buf << "[WARNING] "; 
     436        } 
     437        else if (Level <= Log::NOTICE) 
     438        { 
     439                buf << "[NOTICE]  "; 
     440        } 
     441        else if (Level <= Log::INFO) 
     442        { 
     443                buf << "[INFO]    "; 
     444        } 
     445        else if (Level <= Log::TRACE) 
     446        { 
     447                buf << "[TRACE]   "; 
     448        } 
     449 
     450        buf << rMessage << "\n"; 
     451        std::string output = buf.str(); 
     452 
     453        #ifdef WIN32 
     454                ConvertUtf8ToConsole(output.c_str(), output); 
     455        #endif 
     456 
     457        mLogFile.Write(output.c_str(), output.length()); 
     458 
     459        Logging::Add(this); 
     460        return true; 
     461} 
  • box/trunk/lib/common/Logging.h

    r2279 r2296  
    1515#include <sstream> 
    1616#include <vector> 
     17 
     18#include "FileStream.h" 
    1719 
    1820/* 
     
    116118        public: 
    117119        Logger(); 
     120        Logger(Log::Level level); 
    118121        virtual ~Logger(); 
    119122         
     
    269272}; 
    270273 
     274class FileLogger : public Logger 
     275{ 
     276        private: 
     277        FileStream mLogFile; 
     278        FileLogger(const FileLogger& forbidden) 
     279        : mLogFile("") { /* do not call */ } 
     280         
     281        public: 
     282        FileLogger(const std::string& rFileName, Log::Level Level) 
     283        : Logger(Level), 
     284          mLogFile(rFileName, O_WRONLY | O_CREAT | O_APPEND) 
     285        { } 
     286         
     287        virtual bool Log(Log::Level Level, const std::string& rFile,  
     288                int Line, std::string& rMessage); 
     289         
     290        virtual const char* GetType() { return "FileLogger"; } 
     291        virtual void SetProgramName(const std::string& rProgramName) { } 
     292}; 
     293 
    271294#endif // LOGGING__H 
Note: See TracChangeset for help on using the changeset viewer.