application.cpp
Go to the documentation of this file.
/*
CLAW - a C++ Library Absolutely Wonderful
CLAW is a free library without any particular aim but being useful to
anyone.
Copyright (C) 2005-2011 Julien Jorge
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
contact: julien.jorge@gamned.org
*/
#include <claw/application.hpp>
#include <claw/logger.hpp>
#include <claw/log_stream_uniq.hpp>
#include <claw/log_stream_concise.hpp>
#include <claw/claw_gettext.hpp>
#define CLAW_MK_STR_(e) #e
#define CLAW_MK_STR(e) CLAW_MK_STR_(e)
/*----------------------------------------------------------------------------*/
claw::application::application( int& argc, char** &argv )
: m_arguments( argc, argv )
{
setlocale( LC_ALL, "" );
#ifdef CLAW_TEXT_DOMAIN_PATH
bindtextdomain( "libclaw", CLAW_MK_STR(CLAW_TEXT_DOMAIN_PATH) );
#endif
bind_textdomain_codeset( "libclaw", "UTF-8" );
textdomain("libclaw");
m_arguments.add_long
("--log-file", claw_gettext("The file to use to store log information."),
true, claw_gettext("file") );
m_arguments.add_long
("--log-level",
claw_gettext("Level of log information:\n"
"\t\terror: error messages,\n"
"\t\twarning: warning and error messages,\n"
"\t\tverbose: all messages."), true, claw_gettext("string") );
m_arguments.add_long
("--log-uniq",
claw_gettext
("Use a logger that does not output successively the same message."),
true );
m_arguments.add_long
("--log-concise",
claw_gettext
("Use a logger that does not output messages that have been recently"
" output."), true, claw_gettext("integer") );
m_arguments.parse( argc, argv );
log_stream* log;
if ( m_arguments.has_value("--log-file") )
log = new file_logger( m_arguments.get_string("--log-file") );
else
log = new console_logger;
if ( m_arguments.get_bool("--log-uniq") )
log = new log_stream_uniq(log);
else if ( m_arguments.has_value("--log-concise")
&& m_arguments.only_integer_values("--log-concise")
&& m_arguments.get_integer("--log-concise") > 0 )
log = new log_stream_concise(log, m_arguments.get_integer("--log-concise"));
else if ( m_arguments.get_bool("--log-concise") )
log = new log_stream_concise(log);
logger.set( log );
if ( m_arguments.has_value( "--log-level" ) )
{
std::string level = m_arguments.get_string("--log-level");
if ( (level == "error") || (level == claw_gettext("error")) )
logger.set_level( log_error );
else if ( (level == "warning") || (level == claw_gettext("warning")) )
logger.set_level( log_warning );
else if ( (level == "verbose") || (level == claw_gettext("verbose")) )
logger.set_level( log_verbose );
else
logger.set_level( m_arguments.get_integer("--log-level") );
}
} // application::application()
/*----------------------------------------------------------------------------*/
claw::application::~application()
{
logger.clear();
} // application::~application()
claw::file_logger
This class write log messages in a file.
Definition: log_stream.hpp:84
claw::log_warning
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
claw::application::~application
virtual ~application()
Destructor.
Definition: application.cpp:128
claw::log_system::set_level
CLAW_LOGGER_EXPORT void set_level(int lvl)
Change the level of log.
Definition: logger.cpp:116
claw::log_verbose
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
claw_gettext
#define claw_gettext(s)
Call gettext on the default text domain used by Claw.
Definition: claw_gettext.hpp:39
claw::application::m_arguments
arguments_table m_arguments
The arguments passed by the system.
Definition: application.hpp:70
logger.hpp
Some basic classes for logging.
claw::arguments_table::only_integer_values
bool only_integer_values(const std::string &arg_name) const
Tell if only integer values are associated to an argument.
Definition: arguments_table.cpp:333
claw::log_system::clear
CLAW_LOGGER_EXPORT void clear()
Delete the streams.
Definition: logger.cpp:65
log_stream_uniq.hpp
A log stream that does not output successively the same message.
claw::log_system::set
CLAW_LOGGER_EXPORT void set(stream_type *s)
Set the output stream.
Definition: logger.cpp:105
log_stream_concise.hpp
A log stream that does not output a message that have been recently output.
claw_gettext.hpp
Macros to call gettext on the libclaw textdomain.
claw::console_logger
This class write log messages in std::clog.
Definition: log_stream.hpp:71
claw::log_stream
Base class for streams accepting log output.
Definition: log_stream.hpp:59
claw::arguments_table::get_integer
int get_integer(const std::string &arg_name) const
Get the integer value of an argument.
Definition: arguments_table.cpp:410
claw::log_error
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::arguments_table::parse
void parse(int &argc, char **&argv)
Parse the command line arguments.
Definition: arguments_table.cpp:214
claw::arguments_table::has_value
bool has_value(const std::string &arg_name) const
Tell if an argument has a value.
Definition: arguments_table.cpp:310
claw::logger
log_system logger
The default log system provided by claw.
Definition: logger.cpp:37
claw::application::application
application(int &argc, char **&argv)
Constructor.
Definition: application.cpp:58
claw::arguments_table::get_string
const std::string & get_string(const std::string &arg_name) const
Get the string value of an argument.
Definition: arguments_table.cpp:451
claw::log_stream_uniq
A log stream that does not output successively the same message.
Definition: log_stream_uniq.hpp:58
CLAW_MK_STR
#define CLAW_MK_STR(e)
Build a char[] string representing an expression.
Definition: application.cpp:47
claw::arguments_table::get_bool
bool get_bool(const std::string &arg_name) const
Get the boolean state of an argument.
Definition: arguments_table.cpp:395
claw::arguments_table::add_long
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.
Definition: arguments_table.cpp:180
claw::log_stream_concise
A log stream that does not output a message that have been recently output.
Definition: log_stream_concise.hpp:61
application.hpp
A class to represent the application.