1/* Copyright (c) 2012 Massachusetts Institute of Technology
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to deal
5 * in the Software without restriction, including without limitation the rights
6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 * copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 * THE SOFTWARE.
20 */
21
22#include "Log.h"
23
24#include "Assert.h"
25
26namespace LibUtil
27{
28    using std::ostream;
29    using std::endl;
30
31    Log* Log::msSingleton = NULL;
32    const bool Log::msIsLog = LIBUTIL_IS_LOG;
33
34    void Log::allocate(const String& log_file_name_)
35    {
36        if(msIsLog)
37        {
38            // Allocate static Config instance
39            ASSERT(!msSingleton, "Log singleton is allocated");
40            msSingleton = new Log(log_file_name_);
41        }
42    }
43
44    void Log::release()
45    {
46        if(msIsLog)
47        {
48            ASSERT(msSingleton, "Log singleton is not allocated");
49            delete msSingleton;
50            msSingleton = NULL;
51        }
52        return;
53    }
54
55    void Log::print(const String& str_)
56    {
57        if(msIsLog)
58        {
59            ASSERT(msSingleton, "Log singleton is not allocated");
60            msSingleton->ofs << str_;
61        }
62        return;
63    }
64
65    void Log::print(ostream& stream_, const String& str_)
66    {
67        if(msIsLog)
68        {
69            ASSERT(msSingleton, "Log singleton is not allocated");
70            msSingleton->ofs << str_;
71        }
72        stream_ << str_;
73        return;
74    }
75
76    void Log::printLine(const String& str_)
77    {
78        if(msIsLog)
79        {
80            ASSERT(msSingleton, "Log singleton is not allocated");
81            msSingleton->ofs << str_ << endl;
82        }
83        return;
84    }
85
86    void Log::printLine(ostream& stream_, const String& str_)
87    {
88        if(msIsLog)
89        {
90            ASSERT(msSingleton, "Log singleton is not allocated");
91            msSingleton->ofs << str_ << endl;
92        }
93        stream_ << str_ << endl;
94        return;
95    }
96
97    Log::Log(const String& log_file_name_)
98    {
99        ofs.open(log_file_name_.c_str());
100    }
101
102    Log::~Log()
103    {
104        ofs.close();
105    }
106}
107
108