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
35class GxLogger: public sigc::trackable {
36public:
37 typedef enum {
41 kMessageTypeCount // just count, must be last
43private:
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);
62 friend class GxLoggerGuard;
63public:
66 void print(const char* func, const std::string& msg, MsgType msgtype);
67 void print(const std::string& formatted_msg, MsgType msgtype);
69 static void destroy();
70};
71
72void gx_print_logmsg(const char*, const std::string&, GxLogger::MsgType);
73void gx_print_warning(const char*, const std::string&);
74inline void gx_print_warning(const char* fnc, const boost::basic_format<char>& msg) {
75 gx_print_warning(fnc, msg.str());
76}
77void gx_print_error(const char*, const std::string&);
78inline void gx_print_error(const char* fnc, const boost::basic_format<char>& msg) {
79 gx_print_error(fnc, msg.str());
80}
81void gx_print_fatal(const char*, const std::string&);
82inline void gx_print_fatal(const char* fnc, const boost::basic_format<char>& msg) {
83 gx_print_fatal(fnc, msg.str());
84}
85void gx_print_info(const char*, const std::string&);
86inline void gx_print_info(const char* fnc, const boost::basic_format<char>& msg) {
87 gx_print_info(fnc, msg.str());
88}
89
90class GxFatalError: public std::exception {
91private:
92 std::string msg;
93public:
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
107class GxExit {
108private:
109 sigc::signal<void, bool> exit_sig;
110 pthread_t ui_thread;
111 sigc::signal<void,std::string> message;
112public:
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); }
121};
122
123#endif // SRC_HEADERS_GX_LOGGING_H_
sigc::signal< void, std::string > & signal_msg()
Definition: gx_logging.h:117
static GxExit & get_instance()
sigc::signal< void, bool > exit_sig
Definition: gx_logging.h:109
pthread_t ui_thread
Definition: gx_logging.h:110
sigc::signal< void, std::string > message
Definition: gx_logging.h:111
void set_ui_thread()
Definition: gx_logging.h:115
sigc::signal< void, bool > & signal_exit()
Definition: gx_logging.h:116
void fatal_msg(const std::string &msg)
Definition: gx_logging.h:119
void exit_program(std::string msg="", int errcode=1)
GxFatalError(std::string m)
Definition: gx_logging.h:97
virtual const char * what() const
Definition: gx_logging.h:94
GxFatalError(boost::basic_format< char > &m)
Definition: gx_logging.h:98
std::string msg
Definition: gx_logging.h:92
msg_signal & signal_message()
void print(const char *func, const std::string &msg, MsgType msgtype)
static GxLogger & get_logger()
sigc::signal< void, const std::string &, MsgType, bool > msg_signal
Definition: gx_logging.h:44
void write_queued()
std::string format(const char *func, const std::string &msg)
std::list< logmsg * > msglist
Definition: gx_logging.h:51
@ kWarning
Definition: gx_logging.h:39
@ kMessageTypeCount
Definition: gx_logging.h:41
Glib::Dispatcher * got_new_msg
Definition: gx_logging.h:53
void set_ui_thread()
bool queue_all_msgs
Definition: gx_logging.h:56
static void destroy()
boost::mutex msgmutex
Definition: gx_logging.h:52
void unplug_queue()
msg_signal handlers
Definition: gx_logging.h:55
friend class GxLoggerGuard
Definition: gx_logging.h:62
pthread_t ui_thread
Definition: gx_logging.h:54
void print(const std::string &formatted_msg, MsgType msgtype)
void gx_print_error(const char *, const std::string &)
void gx_print_fatal(const char *, const std::string &)
void gx_print_warning(const char *, const std::string &)
void gx_print_info(const char *, const std::string &)
void gx_print_logmsg(const char *, const std::string &, GxLogger::MsgType)
MsgType msgtype
Definition: gx_logging.h:47
std::string msg
Definition: gx_logging.h:46
logmsg(std::string m, MsgType t, bool p)
Definition: gx_logging.h:49