Log.cc revision 10447
1#include "Log.h"
2
3#include "Assert.h"
4
5namespace LibUtil
6{
7    using std::ostream;
8    using std::endl;
9
10    Log* Log::msSingleton = NULL;
11    const bool Log::msIsLog = LIBUTIL_IS_LOG;
12
13    void Log::allocate(const String& log_file_name_)
14    {
15        if(msIsLog)
16        {
17            // Allocate static Config instance
18            ASSERT(!msSingleton, "Log singleton is allocated");
19            msSingleton = new Log(log_file_name_);
20        }
21    }
22
23    void Log::release()
24    {
25        if(msIsLog)
26        {
27            ASSERT(msSingleton, "Log singleton is not allocated");
28            delete msSingleton;
29            msSingleton = NULL;
30        }
31        return;
32    }
33
34    void Log::print(const String& str_)
35    {
36        if(msIsLog)
37        {
38            ASSERT(msSingleton, "Log singleton is not allocated");
39            msSingleton->ofs << str_;
40        }
41        return;
42    }
43
44    void Log::print(ostream& stream_, const String& str_)
45    {
46        if(msIsLog)
47        {
48            ASSERT(msSingleton, "Log singleton is not allocated");
49            msSingleton->ofs << str_;
50        }
51        stream_ << str_;
52        return;
53    }
54
55    void Log::printLine(const String& str_)
56    {
57        if(msIsLog)
58        {
59            ASSERT(msSingleton, "Log singleton is not allocated");
60            msSingleton->ofs << str_ << endl;
61        }
62        return;
63    }
64
65    void Log::printLine(ostream& stream_, const String& str_)
66    {
67        if(msIsLog)
68        {
69            ASSERT(msSingleton, "Log singleton is not allocated");
70            msSingleton->ofs << str_ << endl;
71        }
72        stream_ << str_ << endl;
73        return;
74    }
75
76    Log::Log(const String& log_file_name_)
77    {
78        ofs.open(log_file_name_.c_str());
79    }
80
81    Log::~Log()
82    {
83        ofs.close();
84    }
85}
86
87