Skip to content

Commit

Permalink
Added --loglevel and --logfile commandline options
Browse files Browse the repository at this point in the history
New command line options added to control logging in Attract-Mode:
  --loglevel (silent|info|debug)
  --logfile <log_file>

`--loglevel silent` suppresses all logging output
`--loglevel info` is the default log level
`--loglevel debug` provides additional debug messages

With the windows GUI version of Attract-Mode, logs are written to
"last_run.log" by default.

For all other versions (linux, MacOS, windows console) logs are
written to the console (stdout) by default.

The --logfile option can be used to log to a specified file
  • Loading branch information
mickelson committed Nov 16, 2017
1 parent 6aacb71 commit 5459f2b
Show file tree
Hide file tree
Showing 23 changed files with 437 additions and 246 deletions.
15 changes: 12 additions & 3 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ line as follows:

attract --config /my/config/location

In the (hopefully unlikely) event that Attract-Mode has difficulty
finding a display font to use on your system, you can specify one at the
command line as follows:
In the (hopefully unlikely) event that Attract-Mode has difficulty finding a
display font to use on your system, you can specify one at the command line as
follows:

attract --font <font_name>

Expand Down Expand Up @@ -254,5 +254,14 @@ For a full description of the command lines options available, run:

`attract --help`

Attract-Mode by default will print log messages to the console window (stdout).
To suppress these messages, run with the following command line:

`attract --loglevel silent`

Alternatively, more verbose debug log messages can be enabled by running:

`attract --loglevel debug`

[Compile.md]: Compile.md
[Layouts.md]: Layouts.md
137 changes: 128 additions & 9 deletions src/fe_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,26 @@

#include "fe_base.hpp"
#include "fe_util.hpp"

#ifndef NO_MOVIE
extern "C"
{
#include <libavutil/log.h>
}
#endif

#ifndef NO_SWF
#include "gameswf/gameswf.h"
#endif

#include <iostream>
#include <fstream>

#define FE_NAME_D "Attract-Mode"

const char *FE_NAME = FE_NAME_D;
const char *FE_COPYRIGHT = FE_NAME_D " " FE_VERSION_D \
" Copyright (c) 2013-2016 Andrew Mickelson";
" Copyright (c) 2013-2017 Andrew Mickelson";
const char *FE_VERSION = FE_VERSION_D;

const char *FE_WHITESPACE=" \t\r";
Expand All @@ -41,6 +53,113 @@ const char *FE_EMULATOR_SUBDIR = "emulators/";
const char *FE_EMULATOR_FILE_EXTENSION = ".cfg";
const char *FE_EMULATOR_DEFAULT = "default-emulator.cfg";

namespace {
std::ofstream g_logfile;
#ifdef SFML_SYSTEM_WINDOWS
std::ofstream g_nullstream( "NUL" );
#else
std::ofstream g_nullstream( "/dev/null" );
#endif
enum FeLogLevel g_log_level=FeLog_Info;

void ffmpeg_log_callback( void *ptr, int level, const char *fmt, va_list vargs )
{
if ( level <= av_log_get_level() )
{
char buff[256];
vsnprintf( buff, 256, fmt, vargs );
FeLog() << "FFmpeg: " << buff;
}
}

void gs_log_callback( bool error, const char *message )
{
FeLog() << "gameswf: " << message;
}
};

std::ostream &FeDebug()
{
if ( g_log_level == FeLog_Debug )
return FeLog();
else
return g_nullstream;
}

std::ostream &FeLog()
{
if ( g_log_level == FeLog_Silent )
return g_nullstream;

if ( g_logfile.is_open() )
return g_logfile;
else
return std::cout;
}

void fe_set_log_file( const std::string &fn )
{
if ( fn.empty() )
g_logfile.close();
else
g_logfile.open( fn.c_str() );
}

void fe_set_log_level( enum FeLogLevel f )
{
g_log_level = f;


#ifndef NO_MOVIE
if ( f == FeLog_Silent )
av_log_set_callback( NULL );
else
{
av_log_set_callback( ffmpeg_log_callback );
av_log_set_level( ( f == FeLog_Debug ) ? AV_LOG_VERBOSE : AV_LOG_ERROR );
}
#endif
#ifndef NO_SWF
if ( f == FeLog_Silent )
gameswf::register_log_callback( NULL );
else
{
gameswf::register_log_callback( gs_log_callback );

gameswf::set_verbose_action( f == FeLog_Debug );
}
#endif
}

void fe_print_version()
{
FeLog() << FE_NAME << " " << FE_VERSION << " ("
<< get_OS_string()
<< ", SFML " << SFML_VERSION_MAJOR << '.' << SFML_VERSION_MINOR
#ifdef USE_FONTCONFIG
<< " +FontConfig"
#endif
#ifdef USE_XINERAMA
<< " +Xinerama"
#endif
#ifdef USE_GLES
<< " +GLES"
#endif
#ifndef NO_SWF
<< " +SWF"
#endif
#ifdef USE_LIBARCHIVE
<< " +7z"
#endif
<< ") " << std::endl;
#ifdef NO_MOVIE
FeLog() << "No Video, using SFML for Audio." << std::endl;
#else
print_ffmpeg_version_info();
#endif

}

void FeBaseConfigurable::invalid_setting(
const std::string & fn,
const char *base,
Expand All @@ -49,29 +168,29 @@ void FeBaseConfigurable::invalid_setting(
const char **valid2,
const char *label )
{
std::cerr << "Unrecognized \"" << base << "\" " << label
<< " of \"" << setting << "\"";
FeLog() << "Unrecognized \"" << base << "\" " << label
<< " of \"" << setting << "\"";

if ( !fn.empty() )
std::cerr << " in file: " << fn;
FeLog() << " in file: " << fn;

std::cerr << ".";
FeLog() << ".";

int i=0;
if (valid1[i])
std::cerr << " Valid " << label <<"s are: " << valid1[i++];
FeLog() << " Valid " << label <<"s are: " << valid1[i++];

while (valid1[i])
std::cerr << ", " << valid1[i++];
FeLog() << ", " << valid1[i++];

if ( valid2 != NULL )
{
i=0;
while (valid2[i])
std::cerr << ", " << valid2[i++];
FeLog() << ", " << valid2[i++];
}

std::cerr << std::endl;
FeLog() << std::endl;
}

bool FeBaseConfigurable::load_from_file( const std::string &filename,
Expand Down
14 changes: 14 additions & 0 deletions src/fe_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#define FE_BASE_HPP

#include <string>
#include <ostream>

extern const char *FE_NAME;
extern const char *FE_COPYRIGHT;
Expand All @@ -36,6 +37,19 @@ extern const char *FE_EMULATOR_SUBDIR;
extern const char *FE_EMULATOR_FILE_EXTENSION;
extern const char *FE_EMULATOR_DEFAULT;

enum FeLogLevel
{
FeLog_Silent,
FeLog_Info,
FeLog_Debug
};

std::ostream &FeLog();
std::ostream &FeDebug();
void fe_set_log_file( const std::string & );
void fe_set_log_level( enum FeLogLevel );
void fe_print_version();

class FeBaseConfigurable
{
protected:
Expand Down
Loading

0 comments on commit 5459f2b

Please sign in to comment.