Claw  1.7.3
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@gamned.org
24 */
30 #include <claw/application.hpp>
31 
32 #include <claw/logger.hpp>
33 #include <claw/log_stream_uniq.hpp>
35 #include <claw/claw_gettext.hpp>
36 
41 #define CLAW_MK_STR_(e) #e
42 
47 #define CLAW_MK_STR(e) CLAW_MK_STR_(e)
48 
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 information."),
70  true, claw_gettext("file") );
72  ("--log-level",
73  claw_gettext("Level of log information:\n"
74  "\t\terror: error messages,\n"
75  "\t\twarning: warning and error messages,\n"
76  "\t\tverbose: all messages."), true, claw_gettext("string") );
78  ("--log-uniq",
80  ("Use a logger that does not output successively the same message."),
81  true );
83  ("--log-concise",
85  ("Use a logger that does not output messages that have been recently"
86  " output."), true, claw_gettext("integer") );
87 
88  m_arguments.parse( argc, argv );
89 
90  log_stream* log;
91 
92  if ( m_arguments.has_value("--log-file") )
93  log = new file_logger( m_arguments.get_string("--log-file") );
94  else
95  log = new console_logger;
96 
97  if ( m_arguments.get_bool("--log-uniq") )
98  log = new log_stream_uniq(log);
99  else if ( m_arguments.has_value("--log-concise")
100  && m_arguments.only_integer_values("--log-concise")
101  && m_arguments.get_integer("--log-concise") > 0 )
102  log = new log_stream_concise(log, m_arguments.get_integer("--log-concise"));
103  else if ( m_arguments.get_bool("--log-concise") )
104  log = new log_stream_concise(log);
105 
106  logger.set( log );
107 
108  if ( m_arguments.has_value( "--log-level" ) )
109  {
110  std::string level = m_arguments.get_string("--log-level");
111 
112  if ( (level == "error") || (level == claw_gettext("error")) )
114  else if ( (level == "warning") || (level == claw_gettext("warning")) )
116  else if ( (level == "verbose") || (level == claw_gettext("verbose")) )
118  else
119  logger.set_level( m_arguments.get_integer("--log-level") );
120  }
121 
122 } // application::application()
123 
124 /*----------------------------------------------------------------------------*/
129 {
130  logger.clear();
131 } // application::~application()
This class write log messages in a file.
Definition: log_stream.hpp:84
bool get_bool(const std::string &arg_name) const
Get the boolean state of an argument.
virtual ~application()
Destructor.
bool has_value(const std::string &arg_name) const
Tell if an argument has a value.
log_level log_error(0,"error: ")
Use this level if something goes really bad and your application may crash.
Definition: log_level.hpp:78
CLAW_LOGGER_EXPORT void set_level(int lvl)
Change the level of log.
Definition: logger.cpp:116
log_level log_verbose(15)
Use this level if you want to inform the user about a situation that is not problematic.
Definition: log_level.hpp:90
#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
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 void clear()
Delete the streams.
Definition: logger.cpp:65
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:105
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:71
Base class for streams accepting log output.
Definition: log_stream.hpp:59
int get_integer(const std::string &arg_name) const
Get the integer value of an argument.
const std::string & get_string(const std::string &arg_name) const
Get the string value of an argument.
void parse(int &argc, char **&argv)
Parse the command line arguments.
log_level log_warning(1,"warning: ")
Use this level if a small problem occurs and you can deal with it without crashing the application...
Definition: log_level.hpp:84
log_system logger
The default log system provided by claw.
Definition: logger.cpp:37
application(int &argc, char **&argv)
Constructor.
Definition: application.cpp:58
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:47
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.