logger.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2016 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 #pragma once
30 
31 #include "string_format.h"
32 #include "string_help.h"
33 #include <mutex>
34 
35 namespace clan
36 {
39 
41  class Logger
42  {
43  public:
45  Logger();
46  virtual ~Logger();
47 
49  static std::vector<Logger*> instances;
50 
52  static std::recursive_mutex mutex;
53 
55  void enable();
56 
58  void disable();
59 
61  virtual void log(const std::string &type, const std::string &text) = 0;
62 
63  protected:
64  static StringFormat get_log_string(const std::string &type, const std::string &text);
65  };
66 
69  void log_event(const std::string &type, const std::string &text);
70 
71  template <class Arg1>
72  void log_event(const std::string &type, const std::string &format, Arg1 arg1)
73  {
74  StringFormat f(format); f.set_arg(1, arg1); log_event(type, f.get_result());
75  }
76 
77  template <class Arg1, class Arg2>
78  void log_event(const std::string &type, const std::string &format, Arg1 arg1, Arg2 arg2)
79  {
80  StringFormat f(format); f.set_arg(1, arg1); f.set_arg(2, arg2); log_event(type, f.get_result());
81  }
82 
83  template <class Arg1, class Arg2, class Arg3>
84  void log_event(const std::string &type, const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3)
85  {
86  StringFormat f(format); f.set_arg(1, arg1); f.set_arg(2, arg2); f.set_arg(3, arg3); log_event(type, f.get_result());
87  }
88 
89  template <class Arg1, class Arg2, class Arg3, class Arg4>
90  void log_event(const std::string &type, const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4)
91  {
92  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());
93  }
94 
95  template <class Arg1, class Arg2, class Arg3, class Arg4, class Arg5>
96  void log_event(const std::string &type, const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5)
97  {
98  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());
99  }
100 
101  template <class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6>
102  void log_event(const std::string &type, const std::string &format, Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5, Arg6 arg6)
103  {
104  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());
105  }
106 
107  template <class Arg1, class Arg2, class Arg3, class Arg4, class Arg5, class Arg6, class Arg7>
108  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)
109  {
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); f.set_arg(6, arg6); f.set_arg(7, arg7); log_event(type, f.get_result());
111  }
112 
114 }
115 
Definition: clanapp.h:35
void set_arg(int index, const std::string &text)
Sets an argument (string version)
void log_event(const std::string &type, const std::string &text)
Log text to logger.
void disable()
Disable logging.
Logger interface.
Definition: logger.h:41
Logger()
Constructs a logger.
void enable()
Enable logger for logging.
static std::recursive_mutex mutex
Logger mutex object.
Definition: logger.h:52
static StringFormat get_log_string(const std::string &type, const std::string &text)
const std::string & get_result() const
Retrieves the formatted string with all argument replacements.
virtual ~Logger()
static std::vector< Logger * > instances
Pointers to currently enabled logger.
Definition: logger.h:49
virtual void log(const std::string &type, const std::string &text)=0
Log text.
String formatting class.
Definition: string_format.h:70