1 // MIT License
2 //
3 // Copyright (c) 2018 Jussi Lind <jussi.lind@iki.fi>
4 //
5 // https://github.com/juzzlin/SimpleLogger
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy
8 // of this software and associated documentation files (the "Software"), to deal
9 // in the Software without restriction, including without limitation the rights
10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 // copies of the Software, and to permit persons to whom the Software is
12 // furnished to do so, subject to the following conditions:
13 //
14 // The above copyright notice and this permission notice shall be included in all
15 // copies or substantial portions of the Software.
16 //
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 // SOFTWARE.
24 
25 #ifndef JUZZLIN_LOGGER_HPP
26 #define JUZZLIN_LOGGER_HPP
27 
28 #include <cstdio>
29 #include <memory>
30 #include <sstream>
31 
32 namespace juzzlin {
33 
34 /*!
35  * Example initialization:
36  *
37  * using juzzlin::L;
38  *
39  * L::init("myLog.txt");
40  *
41  * Example logging:
42  *
43  * L().info() << "Initialization finished.";
44  * L().error() << "Foo happened!";
45  */
46 class Logger
47 {
48 public:
49     enum class Level
50     {
51         Trace,
52         Debug,
53         Info,
54         Warning,
55         Error,
56         Fatal,
57         None
58     };
59 
60     enum class TimestampMode
61     {
62         None,
63         DateTime,
64         EpochSeconds,
65         EpochMilliseconds,
66         EpochMicroseconds
67     };
68 
69     //! Constructor.
70     Logger();
71 
72     //! Destructor.
73     ~Logger();
74 
75     /*! Initialize the logger.
76      *  \param filename Log to filename. Disabled if empty.
77      *  \param append The existing log will be appended if true.
78      *  Throws on error. */
79     static void init(std::string filename, bool append = false);
80 
81     //! Enable/disable echo mode.
82     //! \param enable Echo everything if true. Default is false.
83     static void enableEchoMode(bool enable);
84 
85     //! Set the logging level.
86     //! \param level The minimum level. Default is Info.
87     static void setLoggingLevel(Level level);
88 
89     //! Set custom symbol for the given logging level.
90     //! \param level The level.
91     //! \param symbol The symbol outputted for the messages of this level.
92     static void setLevelSymbol(Level level, std::string symbol);
93 
94     //! Set/enable timestamp mode.
95     //! \param timestampMode Timestamp mode enumeration.
96     //! \param separator Separator string outputted after timestamp.
97     static void setTimestampMode(TimestampMode timestampMode, std::string separator = " ");
98 
99     //! Set specific stream.
100     //! \param level The level.
101     //! \param stream The output stream.
102     static void setStream(Level level, std::ostream & stream);
103 
104     //! \return Library version in x.y.z
105     static std::string version();
106 
107     //! Get stream to the trace log message.
108     std::ostringstream & trace();
109 
110     //! Get stream to the debug log message.
111     std::ostringstream & debug();
112 
113     //! Get stream to the info log message.
114     std::ostringstream & info();
115 
116     //! Get stream to the warning log message.
117     std::ostringstream & warning();
118 
119     //! Get stream to the error log message.
120     std::ostringstream & error();
121 
122     //! Get stream to the fatal log message.
123     std::ostringstream & fatal();
124 
125 private:
126     Logger(const Logger & r) = delete;
127     Logger & operator=(const Logger & r) = delete;
128 
129     class Impl;
130     std::unique_ptr<Impl> m_impl;
131 };
132 
133 using L = Logger;
134 
135 } // juzzlin
136 
137 #endif // JUZZLIN_LOGGER_HPP
138