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 {
38  kInfo,
40  kError,
41  kMessageTypeCount // just count, must be last
43 private:
44  typedef sigc::signal<void, const std::string&, MsgType, bool> msg_signal;
45  struct logmsg {
46  std::string msg;
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;
57  std::string format(const char* func, const std::string& msg);
58  void set_ui_thread();
61  void write_queued();
62  friend class GxLoggerGuard;
63 public:
64  void unplug_queue();
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:
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_
gx_print_fatal
void gx_print_fatal(const char *, const std::string &)
GxLogger::logmsg
Definition: gx_logging.h:47
gx_print_info
void gx_print_info(const char *, const std::string &)
GxLogger::print
void print(const char *func, const std::string &msg, MsgType msgtype)
GxLogger::logmsg::plugged
bool plugged
Definition: gx_logging.h:50
GxFatalError::GxFatalError
GxFatalError(boost::basic_format< char > &m)
Definition: gx_logging.h:98
GxExit
Definition: gx_logging.h:107
GxLogger
Definition: gx_logging.h:35
GxLogger::msgmutex
boost::mutex msgmutex
Definition: gx_logging.h:54
GxLogger::format
std::string format(const char *func, const std::string &msg)
gx_print_warning
void gx_print_warning(const char *, const std::string &)
GxLogger::destroy
static void destroy()
GxLogger::logmsg::msgtype
MsgType msgtype
Definition: gx_logging.h:49
GxLogger::get_logger
static GxLogger & get_logger()
GxLogger::kInfo
@ kInfo
Definition: gx_logging.h:42
GxLogger::msg_signal
sigc::signal< void, const std::string &, MsgType, bool > msg_signal
Definition: gx_logging.h:46
GxLogger::unplug_queue
void unplug_queue()
GxLogger::logmsg::msg
std::string msg
Definition: gx_logging.h:48
GxLogger::GxLoggerGuard
friend class GxLoggerGuard
Definition: gx_logging.h:64
GxLogger::handlers
msg_signal handlers
Definition: gx_logging.h:57
GxFatalError::GxFatalError
GxFatalError(std::string m)
Definition: gx_logging.h:97
GxLogger::ui_thread
pthread_t ui_thread
Definition: gx_logging.h:56
gx_print_error
void gx_print_error(const char *, const std::string &)
GxLogger::msglist
std::list< logmsg * > msglist
Definition: gx_logging.h:53
GxLogger::logmsg::logmsg
logmsg(std::string m, MsgType t, bool p)
Definition: gx_logging.h:51
gx_print_logmsg
void gx_print_logmsg(const char *, const std::string &, GxLogger::MsgType)
GxLogger::got_new_msg
Glib::Dispatcher * got_new_msg
Definition: gx_logging.h:55
GxLogger::write_queued
void write_queued()
GxLogger::kMessageTypeCount
@ kMessageTypeCount
Definition: gx_logging.h:45
GxLogger::kWarning
@ kWarning
Definition: gx_logging.h:43
GxFatalError::what
virtual const char * what() const
Definition: gx_logging.h:94
GxFatalError
Definition: gx_logging.h:90
GxLogger::set_ui_thread
void set_ui_thread()
GxLogger::signal_message
msg_signal & signal_message()
GxFatalError::~GxFatalError
~GxFatalError()
GxLogger::kError
@ kError
Definition: gx_logging.h:44
GxFatalError::msg
std::string msg
Definition: gx_logging.h:92
GxLogger::MsgType
MsgType
Definition: gx_logging.h:39
GxLogger::queue_all_msgs
bool queue_all_msgs
Definition: gx_logging.h:58
GxLogger::GxLogger
GxLogger()
GxLogger::~GxLogger
~GxLogger()