claw  1.9.0
application.cpp
Go to the documentation of this file.
1 /*
2  CLAW - a C++ Library Absolutely Wonderful
3 
4  CLAW is a free library without any particular aim but being useful to
5  anyone.
6 
7  Copyright (C) 2005-2011 Julien Jorge
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 
23  contact: julien.jorge@stuff-o-matic.com
24 */
31 
34 #include <claw/logger/logger.hpp>
35 
36 #include <claw/claw_gettext.hpp>
37 
42 #define CLAW_MK_STR_(e) #e
43 
48 #define CLAW_MK_STR(e) CLAW_MK_STR_(e)
49 
58 claw::application::application(int& argc, char**& argv)
59  : m_arguments(argc, argv)
60 {
61  setlocale(LC_ALL, "");
62 #ifdef CLAW_TEXT_DOMAIN_PATH
63  bindtextdomain("libclaw", CLAW_MK_STR(CLAW_TEXT_DOMAIN_PATH));
64 #endif
65  bind_textdomain_codeset("libclaw", "UTF-8");
66  textdomain("libclaw");
67 
69  "--log-file", claw_gettext("The file to use to store log informations."),
70  true, claw_gettext("file"));
72  "--log-level",
73  claw_gettext("Level of log informations:\n"
74  "\t\terror: error messages,\n"
75  "\t\twarning: warning and error messages,\n"
76  "\t\tverbose: all messages."),
77  true, claw_gettext("string"));
79  "--log-uniq",
81  "Use a logger that does not output successively the same message."),
82  true);
84  "--log-concise",
86  "Use a logger that does not output messages that have been recently"
87  " output."),
88  true, claw_gettext("integer"));
89 
90  m_arguments.parse(argc, argv);
91 
92  log_stream* log;
93 
94  if(m_arguments.has_value("--log-file"))
95  log = new file_logger(m_arguments.get_string("--log-file"));
96  else
97  log = new console_logger;
98 
99  if(m_arguments.get_bool("--log-uniq"))
100  log = new log_stream_uniq(log);
101  else if(m_arguments.has_value("--log-concise")
102  && m_arguments.only_integer_values("--log-concise")
103  && m_arguments.get_integer("--log-concise") > 0)
104  log = new log_stream_concise(log,
105  m_arguments.get_integer("--log-concise"));
106  else if(m_arguments.get_bool("--log-concise"))
107  log = new log_stream_concise(log);
108 
109  logger.set(log);
110 
111  if(m_arguments.has_value("--log-level"))
112  {
113  std::string level = m_arguments.get_string("--log-level");
114 
115  if((level == "error") || (level == claw_gettext("error")))
117  else if((level == "warning") || (level == claw_gettext("warning")))
119  else if((level == "verbose") || (level == claw_gettext("verbose")))
121  else
122  logger.set_level(m_arguments.get_integer("--log-level"));
123  }
124 }
125 
130 {
131  logger.clear();
132 }
This class write log messages in a file.
Definition: log_stream.hpp:85
virtual ~application()
Destructor.
CLAW_LOGGER_EXPORT void set_level(int lvl)
Change the level of log.
Definition: logger.cpp:117
CLAW_LOGGER_EXPORT log_level log_verbose
Use this level if you want to inform the user about a situation that is not problematic.
#define claw_gettext(s)
Call gettext on the default text domain used by Claw.
arguments_table m_arguments
The arguments passed by the system.
Definition: application.hpp:70
CLAW_LOGGER_EXPORT log_system logger
The default log system provided by claw.
Definition: logger.cpp:37
Some basic classes for logging.
bool only_integer_values(const std::string &arg_name) const
Tell if only integer values are associated to an argument.
CLAW_LOGGER_EXPORT log_level log_error
Use this level if something goes really bad and your application may crash.
CLAW_LOGGER_EXPORT void clear()
Delete the streams.
Definition: logger.cpp:61
A log stream that does not output successively the same message.
CLAW_LOGGER_EXPORT void set(stream_type *s)
Set the output stream.
Definition: logger.cpp:104
A log stream that does not output a message that have been recently output.
Macros to call gettext on the libclaw textdomain.
This class write log messages in std::clog.
Definition: log_stream.hpp:72
Base class for streams accepting log output.
Definition: log_stream.hpp:60
int get_integer(const std::string &arg_name) const
Get the integer value of an argument.
void parse(int &argc, char **&argv)
Parse the command line arguments.
CLAW_LOGGER_EXPORT log_level log_warning
Use this level if a small problem occurs and you can deal with it without crashing the application...
bool has_value(const std::string &arg_name) const
Tell if an argument has a value.
application(int &argc, char **&argv)
Constructor.
Definition: application.cpp:58
const std::string & get_string(const std::string &arg_name) const
Get the string value of an argument.
A log stream that does not output successively the same message.
#define CLAW_MK_STR(e)
Build a char[] string representing an expression.
Definition: application.cpp:48
bool get_bool(const std::string &arg_name) const
Get the boolean state of an argument.
void add_long(const std::string &long_name, const std::string &help_msg="", bool optional=false, const std::string &val_name="")
Add an argument in the table.
A log stream that does not output a message that have been recently output.
A class to represent the application.