1 | #include "mars_log.h"
|
---|
2 |
|
---|
3 | #include <iomanip>
|
---|
4 | #include <time.h>
|
---|
5 |
|
---|
6 | namespace mars
|
---|
7 | {
|
---|
8 | Log g_log;
|
---|
9 |
|
---|
10 | Log::Log( const std::string & sFilename /*= "mars.log"*/, const Level enLevel /*= Fatal*/, const unsigned int enSettings /*= Function | Time*/ )
|
---|
11 | : _outfile(sFilename), _tTimeStart(time(NULL)), _enSettings(enSettings), _enLevel(enLevel)
|
---|
12 | {
|
---|
13 | _outfile << "Log started, be nice" << std::endl;
|
---|
14 | }
|
---|
15 |
|
---|
16 | Log::~Log()
|
---|
17 | {
|
---|
18 | _outfile.close();
|
---|
19 | }
|
---|
20 |
|
---|
21 | mars::Outputter Log::stream( const Level enLevel /*= Log::Info*/, const char * szFile /*= NULL*/, const char * szFunction /*= NULL*/, const unsigned int nLine /*= 0*/ )
|
---|
22 | {
|
---|
23 | if (enLevel <= _enLevel)
|
---|
24 | return Outputter(&_outfile, _enSettings, &_mutex, time(NULL) - _tTimeStart, szFile, nLine, szFunction, boost::this_thread::get_id());
|
---|
25 | else
|
---|
26 | return Outputter();
|
---|
27 | }
|
---|
28 |
|
---|
29 | Log & Log::getSingleton()
|
---|
30 | {
|
---|
31 | return g_log;
|
---|
32 | }
|
---|
33 |
|
---|
34 | Outputter::Outputter() : _pOut(NULL), _pMutex(NULL) {}
|
---|
35 |
|
---|
36 | Outputter::Outputter( std::ofstream * pOut, const unsigned int enSettings, boost::mutex * pMutex, const time_t & tTime, const char * szFile, const unsigned int nLine, const char * szFunction, const boost::thread::id & idThread )
|
---|
37 | : _pOut (pOut), _pMutex(pMutex)
|
---|
38 | {
|
---|
39 | _pMutex->lock();
|
---|
40 |
|
---|
41 | if (enSettings & Log_Time)
|
---|
42 | *_pOut
|
---|
43 | << std::put_time(std::localtime(&tTime), "HMS");
|
---|
44 |
|
---|
45 | if ((enSettings & Log_File) != 0 && szFile != NULL)
|
---|
46 | *_pOut << " " << szFile;
|
---|
47 | if ((enSettings & Log_Line) != 0 && nLine > 0)
|
---|
48 | *_pOut << " (" << nLine << ")";
|
---|
49 | if ((enSettings & Log_Function) != 0 && szFunction != NULL)
|
---|
50 | *_pOut << " " << szFunction;
|
---|
51 | if ((enSettings & Log_Thread))
|
---|
52 | *_pOut << " - [thread:" << idThread << "]";
|
---|
53 |
|
---|
54 | *_pOut << ": \t";
|
---|
55 | }
|
---|
56 |
|
---|
57 | Outputter::~Outputter()
|
---|
58 | {
|
---|
59 | if (_pOut != NULL && _pMutex != NULL)
|
---|
60 | {
|
---|
61 | *_pOut << std::endl;
|
---|
62 | _pOut->flush();
|
---|
63 | _pMutex->unlock();
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | } |
---|