]> git.armaanb.net Git - gen-shell.git/blob - src/libshared/src/Log.cpp
added install instructions
[gen-shell.git] / src / libshared / src / Log.cpp
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright 2015 - 2017, Paul Beckingham, Federico Hernandez.
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included
13 // in all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 //
23 // http://www.opensource.org/licenses/mit-license.php
24 //
25 ////////////////////////////////////////////////////////////////////////////////
26
27 #include <cmake.h>
28 #include <Log.h>
29 #include <shared.h>
30 #include <format.h>
31
32 ////////////////////////////////////////////////////////////////////////////////
33 std::string Log::file () const
34 {
35   return _name;
36 }
37
38 ////////////////////////////////////////////////////////////////////////////////
39 void Log::file (const std::string& name)
40 {
41   _name = name;
42   _file = File (_name);
43 }
44
45 ////////////////////////////////////////////////////////////////////////////////
46 void Log::ignore (const std::string& category)
47 {
48   _ignore.insert (category);
49 }
50
51 ////////////////////////////////////////////////////////////////////////////////
52 void Log::write (const std::string& line)
53 {
54   write ("info", line);
55 }
56
57 ////////////////////////////////////////////////////////////////////////////////
58 void Log::write (const std::string& category, const std::string& line)
59 {
60   // Determine if this category is in the ignore list.
61   if (_ignore.find (category) != _ignore.end ())
62     return;
63
64   if (line != "")
65   {
66     // If line contains newlines, split it into separate lines and log each one.
67     if (line.find ('\n') != std::string::npos)
68     {
69       for (const auto& single : split (line, '\n'))
70         write (category, single);
71
72       return;
73     }
74
75     // Single line.
76     if (line == _prior)
77     {
78       ++_repetition;
79     }
80     else
81     {
82       _prior = line;
83
84       // Catch up by writing out the backlog.
85       if (_name != "" && _backlog.size ())
86       {
87         for (const auto& line : _backlog)
88           _file.append (line); 
89
90         _backlog.clear ();
91       }
92
93       if (_repetition)
94       {
95         std::string message = Datetime ().toISO ()
96                             + ' ' + PACKAGE_VERSION
97                             + ' ' + category
98                             + ' ' + format ("(Repeated {1} times)", _repetition)
99                             + '\n';
100
101         if (_name != "")
102           _file.append (message);
103         else
104           _backlog.push_back (message);
105
106         _repetition = 0;
107       }
108
109       std::string message = Datetime ().toISO ()
110                           + ' ' + PACKAGE_VERSION
111                           + ' ' + category
112                           + ' ' + line
113                           + '\n';
114
115       if (_name != "")
116         _file.append (message);
117       else
118         _backlog.push_back (message);
119     }
120   }
121 }
122
123 ////////////////////////////////////////////////////////////////////////////////