Пример #1
0
void StLogger::write(const StString&       theMessage,
                     const StLogger::Level theLevel,
                     const StLogContext*   ) {
    if(theLevel > myFilter || theMessage.isEmpty()) {
        // just ignore
        return;
    }

    // lock for safety
    if(!myMutex.isNull()) {
        myMutex->lock();
    }

    // log to the file
    if(!myFilePath.isEmpty()) {
    #ifdef _WIN32
        myFileHandle = _wfopen(myFilePath.toCString(), L"ab");
    #elif defined(__linux__)
        myFileHandle =   fopen(myFilePath.toCString(),  "ab");
    #endif
        if(myFileHandle != NULL) {
            switch(theLevel) {
                case ST_PANIC:
                    fwrite("PANIC !! ", 1, 9, myFileHandle);
                    fwrite(theMessage.toCString(), 1, theMessage.getSize(), myFileHandle);
                    break;
                case ST_FATAL:
                    fwrite("FATAL !! ", 1, 9, myFileHandle);
                    fwrite(theMessage.toCString(), 1, theMessage.getSize(), myFileHandle);
                    break;
                case ST_ERROR:
                    fwrite("ERROR !! ", 1, 9, myFileHandle);
                    fwrite(theMessage.toCString(), 1, theMessage.getSize(), myFileHandle);
                    break;
                case ST_WARNING:
                    fwrite("WARN  -- ", 1, 9, myFileHandle);
                    fwrite(theMessage.toCString(), 1, theMessage.getSize(), myFileHandle);
                    break;
                case ST_INFO:
                case ST_VERBOSE:
                    fwrite("INFO  -- ", 1, 9, myFileHandle);
                    fwrite(theMessage.toCString(), 1, theMessage.getSize(), myFileHandle);
                    break;
                case ST_DEBUG:
                    fwrite("DEBUG -- ", 1, 9, myFileHandle);
                    fwrite(theMessage.toCString(), 1, theMessage.getSize(), myFileHandle);
                    break;
                default:
                    fwrite(theMessage.toCString(), 1, theMessage.getSize(), myFileHandle);
                    break;
            }
            fwrite("\n", 1, 1, myFileHandle);
            fclose(myFileHandle);
            myFileHandle = NULL;
        }
    }

    // log to standard output (with colored prefix)
    if(myToLogCout) {
        switch(theLevel) {
            case ST_PANIC:
                ST_LOG_CERR << st::COLOR_FOR_RED      << stostream_text("PANIC !! ") << st::COLOR_FOR_WHITE << theMessage << stostream_text('\n');
                break;
            case ST_FATAL:
                ST_LOG_CERR << st::COLOR_FOR_RED      << stostream_text("FATAL !! ") << st::COLOR_FOR_WHITE << theMessage << stostream_text('\n');
                break;
            case ST_ERROR:
                ST_LOG_CERR << st::COLOR_FOR_RED      << stostream_text("ERROR !! ") << st::COLOR_FOR_WHITE << theMessage << stostream_text('\n');
                break;
            case ST_WARNING:
                ST_LOG_CERR << st::COLOR_FOR_YELLOW_L << stostream_text("WARN  -- ") << st::COLOR_FOR_WHITE << theMessage << stostream_text('\n');
                break;
            case ST_INFO:
            case ST_VERBOSE:
                ST_LOG_CERR << st::COLOR_FOR_YELLOW_L << stostream_text("INFO  -- ") << st::COLOR_FOR_WHITE << theMessage << stostream_text('\n');
                break;
            case ST_DEBUG:
                ST_LOG_CERR << st::COLOR_FOR_YELLOW_L << stostream_text("DEBUG -- ") << st::COLOR_FOR_WHITE << theMessage << stostream_text('\n');
                break;
            default:
                ST_LOG_CERR << theMessage << stostream_text('\n');
                break;
        }
    }

    // log to the system journal(s)
/*#ifdef _WIN32
    // get a handle to the event log
    HANDLE anEventLog = RegisterEventSource(NULL,      // local computer
                                            L"sView"); // event source name
    if(anEventLog != NULL) {
        WORD aLogType = 0;
        switch(theLevel) {
            case ST_PANIC:
            case ST_FATAL:
            case ST_ERROR:
                aLogType = EVENTLOG_ERROR_TYPE;
                break;
            case ST_WARNING:
                aLogType = EVENTLOG_WARNING_TYPE;
                break;
            case ST_INFO:
            case ST_VERBOSE:
            case ST_DEBUG:
            default:
                aLogType = EVENTLOG_INFORMATION_TYPE;
                break;
        }
        ReportEvent(anEventLog, aLogType,
                    0,               // event category
                    0,               // event identifier
                    NULL,            // no user security identifier
                    1,               // number of substitution strings
                    0,               // no data
                    (LPCWSTR* )&theMessage.utfText(), // pointer to strings
                    NULL))           // no binary data
        DeregisterEventSource(anEventLog);
    }
#endif*/

    // unlock mutex
    if(!myMutex.isNull()) {
        myMutex->unlock();
    }
}