Changeset 2081

Show
Ignore:
Timestamp:
31/01/2008 23:44:54 (2 years ago)
Author:
chris
Message:

Add support for microsecond timestamps and PID logging on console log
for daemons.

Location:
box/trunk/lib
Files:
3 modified

Legend:

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

    r2001 r2081  
    183183bool Console::sShowTimeMicros = false; 
    184184bool Console::sShowTag = false; 
     185bool Console::sShowPID = false; 
    185186std::string Console::sTag; 
    186187 
     
    201202} 
    202203 
     204void Console::SetShowPID(bool enabled) 
     205{ 
     206        sShowPID = enabled; 
     207} 
     208 
    203209bool Console::Log(Log::Level level, const std::string& rFile,  
    204210        int line, std::string& rMessage) 
     
    216222        } 
    217223 
    218         std::string msg; 
     224        std::ostringstream buf; 
    219225 
    220226        if (sShowTime) 
     
    232238                #endif 
    233239                { 
    234                         std::ostringstream buf; 
    235  
    236240                        buf << std::setfill('0') << 
    237241                                std::setw(2) << tm_ptr->tm_hour << ":" <<  
     
    245249 
    246250                        buf << " "; 
    247                         msg += buf.str(); 
    248251                } 
    249252                else 
    250253                { 
    251                         msg += strerror(errno); 
    252                         msg += " "; 
     254                        buf << strerror(errno); 
     255                        buf << " "; 
    253256                } 
    254257        } 
     
    256259        if (sShowTag) 
    257260        { 
    258                 msg += "[" + sTag + "] "; 
     261                if (sShowPID) 
     262                { 
     263                        buf << "[" << sTag << " " << getpid() << "] "; 
     264                } 
     265                else 
     266                { 
     267                        buf << "[" << sTag << "] "; 
     268                } 
     269        } 
     270        else if (sShowPID) 
     271        { 
     272                buf << "[" << getpid() << "] "; 
    259273        } 
    260274 
    261275        if (level <= Log::FATAL) 
    262276        { 
    263                 msg += "FATAL: "; 
     277                buf << "FATAL:  "; 
    264278        } 
    265279        else if (level <= Log::ERROR) 
    266280        { 
    267                 msg += "ERROR: "; 
     281                buf << "ERROR:  "; 
    268282        } 
    269283        else if (level <= Log::WARNING) 
    270284        { 
    271                 msg += "WARNING: "; 
     285                buf << "WARNING: "; 
    272286        } 
    273287        else if (level <= Log::NOTICE) 
    274288        { 
    275                 msg += "NOTICE: "; 
    276         } 
    277          
    278         msg += rMessage; 
    279  
    280         fprintf(target, "%s\n", msg.c_str()); 
     289                buf << "NOTICE:  "; 
     290        } 
     291        else if (level <= Log::INFO) 
     292        { 
     293                buf << "INFO:    "; 
     294        } 
     295        else if (level <= Log::TRACE) 
     296        { 
     297                buf << "TRACE:   "; 
     298        } 
     299 
     300        buf << rMessage; 
     301 
     302        fprintf(target, "%s\n", buf.str().c_str()); 
    281303         
    282304        return true; 
  • box/trunk/lib/common/Logging.h

    r2001 r2081  
    122122        static bool sShowTag; 
    123123        static std::string sTag; 
     124        static bool sShowPID; 
    124125 
    125126        public: 
     
    132133        static void SetShowTime(bool enabled); 
    133134        static void SetShowTimeMicros(bool enabled); 
     135        static void SetShowPID(bool enabled); 
    134136}; 
    135137 
  • box/trunk/lib/server/Daemon.cpp

    r2021 r2081  
    107107                "DFk" 
    108108        #endif 
    109                 "hqvVt:T"; 
     109                "hPqvVt:TU"; 
    110110} 
    111111 
     
    126126        "  -k         Keep console open after fork, keep writing log messages to it\n" 
    127127#endif 
     128        "  -P         Show process ID (PID) in console output\n" 
    128129        "  -q         Run more quietly, reduce verbosity level by one, can repeat\n" 
    129130        "  -v         Run more verbosely, increase verbosity level by one, can repeat\n" 
    130131        "  -V         Run at maximum verbosity\n" 
    131132        "  -t <tag>   Tag console output with specified marker\n" 
    132         "  -T         Timestamp console output\n"; 
     133        "  -T         Timestamp console output\n" 
     134        "  -U         Timestamp console output with microseconds\n"; 
    133135} 
    134136 
     
    182184                break; 
    183185 
     186                case 'P': 
     187                { 
     188                        Console::SetShowPID(true); 
     189                } 
     190                break; 
     191 
    184192                case 'q': 
    185193                { 
     
    223231                { 
    224232                        Console::SetShowTime(true); 
     233                } 
     234                break; 
     235 
     236                case 'U': 
     237                { 
     238                        Console::SetShowTime(true); 
     239                        Console::SetShowTimeMicros(true); 
    225240                } 
    226241                break;