]> WPIA git - cassiopeia.git/blob - src/log/logger.hpp
add: Import Logging functionality by Florian Weber
[cassiopeia.git] / src / log / logger.hpp
1 #pragma once
2
3 #include <ostream>
4 #include <string>
5 #include <sstream>
6 #include <vector>
7
8 namespace logger {
9
10     enum class level {
11         debug, note, warn, error, fatal
12     };
13
14     /**
15      * conv::to_string will be used to convert whatever argument is send
16      * to the logger to a string. If another type shall be supported,
17      * just add the appropriate overload and you can start using it right
18      * away. The default conversion will use a stringstream.
19      */
20     namespace conv {
21         template <typename T>
22         inline std::string to_string( const T& arg ) {
23             std::ostringstream stream;
24             stream << arg;
25             return stream.str();
26         }
27         inline std::string to_string( std::string&& arg ) {
28             return arg;
29         }
30         inline std::string to_string( const std::string& arg ) {
31             return arg;
32         }
33         inline std::string to_string( const char* arg ) {
34             return arg;
35         }
36     }
37
38     namespace impl {
39         void log( level, const std::vector<std::string>& args );
40         void logf( level, const std::string&, std::vector<std::string> args );
41     }
42
43     void set_stream( std::ostream& );
44
45     template <typename... Args>
46     void log( level l, Args&& ... data ) {
47         impl::log( l, {conv::to_string( std::forward<Args>( data ) )...} );
48     }
49
50     template <typename... Args>
51     void logf( level l, const std::string& format, Args&& ... data ) {
52         impl::logf( l, format, {conv::to_string( std::forward<Args>( data ) )...} );
53     }
54
55     template <typename... Args>
56     void debug( Args&& ... args ) {
57         log( level::debug, std::forward<Args>( args )... );
58     }
59     template <typename... Args>
60     void note( Args&& ... args ) {
61         log( level::note, std::forward<Args>( args )... );
62     }
63     template <typename... Args>
64     void warn( Args&& ... args ) {
65         log( level::warn, std::forward<Args>( args )... );
66     }
67     template <typename... Args>
68     void error( Args&& ... args ) {
69         log( level::error, std::forward<Args>( args )... );
70     }
71     template <typename... Args>
72     void fatal( Args&& ... args ) {
73         log( level::fatal, std::forward<Args>( args )... );
74     }
75
76     template <typename... Args>
77     void debugf( const std::string& fmt, Args&& ... args ) {
78         logf( level::debug, fmt, std::forward<Args>( args )... );
79     }
80     template <typename... Args>
81     void notef( const std::string& fmt, Args&& ... args ) {
82         logf( level::note, fmt, std::forward<Args>( args )... );
83     }
84     template <typename... Args>
85     void warnf( const std::string& fmt, Args&& ... args ) {
86         logf( level::warn, fmt, std::forward<Args>( args )... );
87     }
88     template <typename... Args>
89     void errorf( const std::string& fmt, Args&& ... args ) {
90         logf( level::error, fmt, std::forward<Args>( args )... );
91     }
92     template <typename... Args>
93     void fatalf( const std::string& fmt, Args&& ... args ) {
94         logf( level::fatal, fmt, std::forward<Args>( args )... );
95     }
96
97 }