Guitarix
gx_logging.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009, 2010, 2013 Hermann Meyer, James Warden, Andreas Degert
3  * Copyright (C) 2011 Pete Shorthose
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  * --------------------------------------------------------------------------
19  */
20 
21 #pragma once
22 
23 #ifndef SRC_HEADERS_GX_LOGGING_H_
24 #define SRC_HEADERS_GX_LOGGING_H_
25 
26 #include <glib/gi18n.h>
27 #include <glibmm/dispatcher.h>
28 #include <boost/thread/mutex.hpp>
29 #include <boost/format.hpp>
30 
31 /****************************************************************
32  ** Logging
33  */
34 
35 class GxLogger: public sigc::trackable {
36 public:
37  typedef enum {
41  kMessageTypeCount // just count, must be last
42  } MsgType;
43 private:
44  typedef sigc::signal<void, const std::string&, MsgType, bool> msg_signal;
45  struct logmsg {
46  std::string msg;
47  MsgType msgtype;
48  bool plugged;
49  logmsg(std::string m, MsgType t, bool p): msg(m), msgtype(t), plugged(p) {}
50  };
51  std::list<logmsg*> msglist;
52  boost::mutex msgmutex;
53  Glib::Dispatcher* got_new_msg;
54  pthread_t ui_thread;
55  msg_signal handlers;
57  std::string format(const char* func, const std::string& msg);
58  void set_ui_thread();
59  GxLogger();
60  ~GxLogger();
61  void write_queued();
62  friend class GxLoggerGuard;
63 public:
64  void unplug_queue();
65  msg_signal& signal_message();
66  void print(const char* func, const std::string& msg, MsgType msgtype);
67  void print(const std::string& formatted_msg, MsgType msgtype);
68  static GxLogger& get_logger();
69  static void destroy();
70 };
71 
72 void gx_print_logmsg(const char*, const std::string&, GxLogger::MsgType);
73 void gx_print_warning(const char*, const std::string&);
74 inline void gx_print_warning(const char* fnc, const boost::basic_format<char>& msg) {
75  gx_print_warning(fnc, msg.str());
76 }
77 void gx_print_error(const char*, const std::string&);
78 inline void gx_print_error(const char* fnc, const boost::basic_format<char>& msg) {
79  gx_print_error(fnc, msg.str());
80 }
81 void gx_print_fatal(const char*, const std::string&);
82 inline void gx_print_fatal(const char* fnc, const boost::basic_format<char>& msg) {
83  gx_print_fatal(fnc, msg.str());
84 }
85 void gx_print_info(const char*, const std::string&);
86 inline void gx_print_info(const char* fnc, const boost::basic_format<char>& msg) {
87  gx_print_info(fnc, msg.str());
88 }
89 
90 class GxFatalError: public std::exception {
91 private:
92  std::string msg;
93 public:
94  virtual const char* what() const throw() {
95  return msg.c_str();
96  }
97  GxFatalError(std::string m): msg(m) {}
98  GxFatalError(boost::basic_format<char>& m): msg(m.str()) {}
99  ~GxFatalError() throw();
100 };
101 
102 
103 /****************************************************************
104  ** class GxExit
105  */
106 
107 class GxExit {
108 private:
109  sigc::signal<void, bool> exit_sig;
110  pthread_t ui_thread;
111  sigc::signal<void,std::string> message;
112 public:
113  GxExit();
114  ~GxExit();
115  void set_ui_thread() { ui_thread = pthread_self(); }
116  sigc::signal<void, bool>& signal_exit() { return exit_sig; }
117  sigc::signal<void,std::string>& signal_msg() { return message; }
118  void exit_program(std::string msg = "", int errcode = 1);
119  void fatal_msg(const std::string& msg) { message(msg); exit_program(msg); }
120  static GxExit& get_instance();
121 };
122 
123 #endif // SRC_HEADERS_GX_LOGGING_H_
void gx_print_logmsg(const char *, const std::string &, GxLogger::MsgType)
void gx_print_info(const char *, const std::string &)
GxFatalError(std::string m)
Definition: gx_logging.h:97
GxFatalError(boost::basic_format< char > &m)
Definition: gx_logging.h:98
void write_queued()
pthread_t ui_thread
Definition: gx_logging.h:54
void set_ui_thread()
Definition: gx_logging.h:115
msg_signal handlers
Definition: gx_logging.h:55
sigc::signal< void, std::string > & signal_msg()
Definition: gx_logging.h:117
sigc::signal< void, bool > exit_sig
Definition: gx_logging.h:109
void set_ui_thread()
msg_signal & signal_message()
void gx_print_fatal(const char *, const std::string &)
MsgType msgtype
Definition: gx_logging.h:47
bool queue_all_msgs
Definition: gx_logging.h:56
virtual const char * what() const
Definition: gx_logging.h:94
void gx_print_error(const char *, const std::string &)
void fatal_msg(const std::string &msg)
Definition: gx_logging.h:119
pthread_t ui_thread
Definition: gx_logging.h:110
sigc::signal< void, std::string > message
Definition: gx_logging.h:111
std::string msg
Definition: gx_logging.h:46
logmsg(std::string m, MsgType t, bool p)
Definition: gx_logging.h:49
void gx_print_warning(const char *, const std::string &)
boost::mutex msgmutex
Definition: gx_logging.h:52
sigc::signal< void, bool > & signal_exit()
Definition: gx_logging.h:116
sigc::signal< void, const std::string &, MsgType, bool > msg_signal
Definition: gx_logging.h:44
void print(const char *func, const std::string &msg, MsgType msgtype)
friend class GxLoggerGuard
Definition: gx_logging.h:62
static GxLogger & get_logger()
std::string format(const char *func, const std::string &msg)
std::string msg
Definition: gx_logging.h:92
static void destroy()
void unplug_queue()
std::list< logmsg * > msglist
Definition: gx_logging.h:51
Glib::Dispatcher * got_new_msg
Definition: gx_logging.h:53