logger.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2015 The ClanLib Team
4 **
5 ** This software is provided 'as-is', without any express or implied
6 ** warranty. In no event will the authors be held liable for any damages
7 ** arising from the use of this software.
8 **
9 ** Permission is granted to anyone to use this software for any purpose,
10 ** including commercial applications, and to alter it and redistribute it
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries ClanLib may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** Magnus Norddahl
27 */
28 
29 
30 #pragma once
31 
32 #include "string_format.h"
33 #include "string_help.h"
34 #include <mutex>
35 
36 namespace clan
37 {
40 
42 class Logger
43 {
46 
47 public:
49  Logger();
50 
51  virtual ~Logger();
52 
56 
57 public:
59  static std::vector<Logger*> instances;
60 
62  static std::recursive_mutex mutex;
63 
67 
68 public:
70  void enable();
71 
73  void disable();
74 
76  virtual void log(const std::string &type, const std::string &text) = 0;
77 
81 
82 protected:
83  static StringFormat get_log_string(const std::string &type, const std::string &text);
84 
86 };
87 
90 void log_event(const std::string &type, const std::string &text);
91 
92 template <class Arg1>
93 void log_event(const std::string &type, const std::string &format, Arg1 arg1)
94 { StringFormat f(format); f.set_arg(1, arg1); log_event(type, f.get_result()); }
95 
96 template <class Arg1, class Arg2>
97 void log_event(const std::string &type, const std::string &format, Arg1 arg1, Arg2 arg2)
98 { StringFormat f(format); f.set_arg(1, arg1); f.set_arg(2, arg2); log_event(type, f.get_result()); }
99 
100 template <class Arg1, class Arg2, class Arg3>
101 void log_event(const std::string &type, const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3)
102 { StringFormat f(format); f.set_arg(1, arg1); f.set_arg(2, arg2); f.set_arg(3, arg3); log_event(type, f.get_result()); }
103 
104 template <class Arg1, class Arg2, class Arg3, class Arg4>
105 void log_event(const std::string &type, const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4)
106 { StringFormat f(format); f.set_arg(1, arg1); f.set_arg(2, arg2); f.set_arg(3, arg3); f.set_arg(4, arg4); log_event(type, f.get_result()); }
107 
108 template <class Arg1, class Arg2, class Arg3, class Arg4, class Arg5>
109 void log_event(const std::string &type, const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5)
110 { StringFormat f(format); f.set_arg(1, arg1); f.set_arg(2, arg2); f.set_arg(3, arg3); f.set_arg(4, arg4); f.set_arg(5, arg5); log_event(type, f.get_result()); }
111 
112 template <class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6>
113 void log_event(const std::string &type, const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5, Arg6 arg6)
114 { StringFormat f(format); f.set_arg(1, arg1); f.set_arg(2, arg2); f.set_arg(3, arg3); f.set_arg(4, arg4); f.set_arg(5, arg5); f.set_arg(6, arg6); log_event(type, f.get_result()); }
115 
116 template <class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6, class Arg7>
117 void log_event(const std::string &type, const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5, Arg6 arg6, Arg7 arg7)
118 { StringFormat f(format); f.set_arg(1, arg1); f.set_arg(2, arg2); f.set_arg(3, arg3); f.set_arg(4, arg4); f.set_arg(5, arg5); f.set_arg(6, arg6); f.set_arg(7, arg7); log_event(type, f.get_result()); }
119 
120 }
121 
void disable()
Disable logging.
virtual void log(const std::string &type, const std::string &text)=0
Log text.
void log_event(const std::string &type, const std::string &text)
Log text to logger.
Logger interface.
Definition: logger.h:43
static std::recursive_mutex mutex
Logger mutex object.
Definition: logger.h:62
static StringFormat get_log_string(const std::string &type, const std::string &text)
void enable()
Enable logger for logging.
Definition: clanapp.h:36
String formatting class.
Definition: string_format.h:71
virtual ~Logger()
static std::vector< Logger * > instances
Pointers to currently enabled logger.
Definition: logger.h:59
Logger()
Constructs a logger.