Subversion Repositories tpanel

Rev

Rev 94 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 94 Rev 116
Line 116... Line 116...
116
};
116
};
117
#endif
117
#endif
118
 
118
 
119
TStreamError::TStreamError(const string& logFile, const std::string& logLevel)
119
TStreamError::TStreamError(const string& logFile, const std::string& logLevel)
120
{
120
{
-
 
121
    if (!TConfig::isInitialized())
-
 
122
        return;
-
 
123
 
121
    if (!logFile.empty())
124
    if (!logFile.empty())
122
        mLogfile = logFile;
125
        mLogfile = logFile;
123
    else if (!TConfig::getLogFile().empty())
126
    else if (!TConfig::getLogFile().empty())
124
        mLogfile = TConfig::getLogFile();
127
        mLogfile = TConfig::getLogFile();
125
 
128
 
Line 141... Line 144...
141
    }
144
    }
142
}
145
}
143
 
146
 
144
void TStreamError::setLogFile(const std::string &lf)
147
void TStreamError::setLogFile(const std::string &lf)
145
{
148
{
146
    if (mLogfile.compare(lf) == 0)
149
    if (mInitialized && mLogfile.compare(lf) == 0)
147
        return;
150
        return;
148
 
151
 
149
    mLogfile = lf;
152
    mLogfile = lf;
150
    mInitialized = false;
153
    mInitialized = false;
151
    _init();
154
    _init();
Line 169... Line 172...
169
    mLogLevel |= _getLevel(slv.substr(start));
172
    mLogLevel |= _getLevel(slv.substr(start));
170
}
173
}
171
 
174
 
172
bool TStreamError::checkFilter(terrtype_t err)
175
bool TStreamError::checkFilter(terrtype_t err)
173
{
176
{
-
 
177
    if (!TConfig::isInitialized())
-
 
178
        return false;
-
 
179
 
174
    if (err == TERRINFO && (mLogLevel & HLOG_INFO) != 0)
180
    if (err == TERRINFO && (mLogLevel & HLOG_INFO) != 0)
175
        return true;
181
        return true;
176
    else if (err == TERRWARNING && (mLogLevel & HLOG_WARNING) != 0)
182
    else if (err == TERRWARNING && (mLogLevel & HLOG_WARNING) != 0)
177
        return true;
183
        return true;
178
    else if (err == TERRERROR && (mLogLevel & HLOG_ERROR) != 0)
184
    else if (err == TERRERROR && (mLogLevel & HLOG_ERROR) != 0)
Line 185... Line 191...
185
    return false;
191
    return false;
186
}
192
}
187
 
193
 
188
bool TStreamError::checkFilter(int lv)
194
bool TStreamError::checkFilter(int lv)
189
{
195
{
-
 
196
    if (!TConfig::isInitialized())
-
 
197
        return false;
-
 
198
 
190
    if ((mLogLevel & lv) != 0)
199
    if ((mLogLevel & lv) != 0)
191
        return true;
200
        return true;
192
 
201
 
193
    return false;
202
    return false;
194
}
203
}
Line 222... Line 231...
222
    return HLOG_NONE;
231
    return HLOG_NONE;
223
}
232
}
224
 
233
 
225
void TStreamError::_init()
234
void TStreamError::_init()
226
{
235
{
227
    if (mInitialized)
236
    if (!TConfig::isInitialized() || mInitialized)
228
        return;
237
        return;
229
 
238
 
230
    mInitialized = true;
239
    mInitialized = true;
231
 
240
 
232
#if LOGPATH == LPATH_FILE
241
#if LOGPATH == LPATH_FILE
Line 235... Line 244...
235
        try
244
        try
236
        {
245
        {
237
#ifndef __ANDROID__
246
#ifndef __ANDROID__
238
            if (mStream && mStream != &std::cout)
247
            if (mStream && mStream != &std::cout)
239
                delete mStream;
248
                delete mStream;
240
 
-
 
-
 
249
#if __cplusplus < 201402L
241
            mStream = new std::ofstream(mLogfile.c_str(), std::ios::out | std::ios::ate);
250
            mStream = new std::ofstream(mLogfile.c_str(), std::ios::out | std::ios::ate);
242
 
251
#else
-
 
252
            mStream = new std::ofstream(mLogfile, std::ios::out | std::ios::ate);
-
 
253
#endif
243
            if (!mStream || mStream->fail())
254
            if (!mStream || mStream->fail())
244
                mStream = &std::cout;
255
                mStream = &std::cout;
245
#else
256
#else
246
            char *HOME = getenv("HOME");
257
            char *HOME = getenv("HOME");
247
            bool bigLog = false;
258
            bool bigLog = false;
Line 278... Line 289...
278
            std::cerr << "ERROR: " << e.what() << std::endl;
289
            std::cerr << "ERROR: " << e.what() << std::endl;
279
#endif  // __ANDROID__
290
#endif  // __ANDROID__
280
            mStream = &std::cout;
291
            mStream = &std::cout;
281
        }
292
        }
282
    }
293
    }
-
 
294
    else if (!mStream)
283
    else
295
    {
-
 
296
#ifdef __ANDROID__
-
 
297
        std::cout.rdbuf(new androidbuf);
-
 
298
#endif
-
 
299
        mStream = &std::cout;
-
 
300
    }
-
 
301
#else  // LOGPATH == LPATH_FILE
-
 
302
    if (!mStream)
284
    {
303
    {
285
#ifdef __ANDROID__
304
#ifdef __ANDROID__
286
        std::cout.rdbuf(new androidbuf);
305
        std::cout.rdbuf(new androidbuf);
287
#endif
306
#endif
288
        mStream = &std::cout;
307
        mStream = &std::cout;
289
    }
308
    }
290
#else
-
 
291
    std::cout.rdbuf(new androidbuf);
-
 
292
    mStream = &std::cout;
-
 
293
#endif  // LOGPATH == LPATH_FILE
309
#endif  // LOGPATH == LPATH_FILE
294
 
310
 
295
    if (!TConfig::isLongFormat())
311
    if (!TConfig::isLongFormat())
296
        *mStream << "Logfile started at " << getTime() << std::endl;
312
        *mStream << "Logfile started at " << getTime() << std::endl;
297
 
313
 
Line 306... Line 322...
306
    *mStream << "-----------------------------------------------------------------" << std::endl << std::flush;
322
    *mStream << "-----------------------------------------------------------------" << std::endl << std::flush;
307
}
323
}
308
 
324
 
309
void TStreamError::logMsg(std::ostream& str)
325
void TStreamError::logMsg(std::ostream& str)
310
{
326
{
-
 
327
    if (!TConfig::isInitialized())
-
 
328
        return;
-
 
329
 
311
    _init();
330
    _init();
312
 
331
 
313
    if (!mStream || str.fail())
332
    if (!mStream || str.fail())
314
        return;
333
        return;
315
 
334
 
Line 376... Line 395...
376
 
395
 
377
std::mutex tracer_mutex;
396
std::mutex tracer_mutex;
378
 
397
 
379
TTracer::TTracer(const std::string& msg, int line, char *file)
398
TTracer::TTracer(const std::string& msg, int line, char *file)
380
{
399
{
381
    if (!TStreamError::checkFilter(HLOG_TRACE))
400
    if (!TConfig::isInitialized() || !TStreamError::checkFilter(HLOG_TRACE))
382
        return;
401
        return;
383
 
402
 
384
    tracer_mutex.lock();
403
    tracer_mutex.lock();
-
 
404
 
385
    mFile = file;
405
    mFile = file;
386
    size_t pos = mFile.find_last_of("/");
406
    size_t pos = mFile.find_last_of("/");
387
 
407
 
388
    if (pos != string::npos)
408
    if (pos != string::npos)
389
        mFile = mFile.substr(pos + 1);
409
        mFile = mFile.substr(pos + 1);
Line 414... Line 434...
414
    tracer_mutex.unlock();
434
    tracer_mutex.unlock();
415
}
435
}
416
 
436
 
417
TTracer::~TTracer()
437
TTracer::~TTracer()
418
{
438
{
419
    if (!TStreamError::checkFilter(HLOG_TRACE))
439
    if (!TConfig::isInitialized() || !TStreamError::checkFilter(HLOG_TRACE))
420
        return;
440
        return;
421
 
441
 
422
    tracer_mutex.lock();
442
    tracer_mutex.lock();
423
    TError::setErrorType(TERRTRACE);
443
    TError::setErrorType(TERRTRACE);
424
    TError::Current()->decIndent();
444
    TError::Current()->decIndent();
Line 502... Line 522...
502
{
522
{
503
    if (!str || !size)
523
    if (!str || !size)
504
        return;
524
        return;
505
 
525
 
506
    message_mutex.lock();
526
    message_mutex.lock();
-
 
527
 
507
    Current();
528
    if (!Current())
508
    // Save the old format settings in case they were manipulated
-
 
-
 
529
    {
509
//    std::ios oldstate(nullptr);
530
        message_mutex.unlock();
510
//    oldstate.copyfmt(std::cout);
531
        return;
-
 
532
    }
511
    // Print out the message
533
    // Print out the message
512
    std::ostream *stream = mCurrent->getStream();
534
    std::ostream *stream = mCurrent->getStream();
513
    *stream << strToHex(str, size, 16, true, 12) << std::endl;
535
    *stream << strToHex(str, size, 16, true, 12) << std::endl;
514
    *stream << mCurrent->resetFlags(stream);
536
    *stream << mCurrent->resetFlags(stream);
515
//    stream->copyfmt(oldstate);     // Restore old format (reset)
-
 
516
    message_mutex.unlock();
537
    message_mutex.unlock();
517
}
538
}
518
 
539
 
519
string TError::toHex(int num, int width)
540
string TError::toHex(int num, int width)
520
{
541
{
Line 622... Line 643...
622
        default:
643
        default:
623
            prefix = "           ";
644
            prefix = "           ";
624
            mErrType = TERRNONE;
645
            mErrType = TERRNONE;
625
    }
646
    }
626
 
647
 
-
 
648
    if (!TConfig::isInitialized() && (lv == HLOG_ERROR || lv == HLOG_WARNING))
-
 
649
    {
-
 
650
        if (!TConfig::isLongFormat())
-
 
651
            std::cerr << prefix;
-
 
652
        else
-
 
653
            std::cerr << TStreamError::getTime() << " " << prefix << std::setw(20) << " " << ", ";
-
 
654
    }
-
 
655
 
627
    if (!TConfig::isLongFormat())
656
    if (!TConfig::isLongFormat())
628
        return os << prefix;
657
        return os << prefix;
629
    else
658
    else
630
        return os << TStreamError::getTime() << " " << prefix << std::setw(20) << " " << ", ";
659
        return os << TStreamError::getTime() << " " << prefix << std::setw(20) << " " << ", ";
631
}
660
}