Fix #942. Thanks to hilnius.

(note I modified the original patch, and had no opportunity to verify it
again on linux. If it doesn't work, blame me, not hilnius).


git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/trunk@12713 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
hikerstk 2013-04-30 07:17:02 +00:00
parent 0a8f018664
commit e99e944f9b
5 changed files with 94 additions and 46 deletions

View File

@ -553,9 +553,17 @@ namespace UserConfigParams
// TODO : is this used with new code? does it still work?
PARAM_PREFIX BoolUserConfigParam m_crashed
PARAM_DEFAULT( BoolUserConfigParam(false, "crashed") );
PARAM_PREFIX BoolUserConfigParam m_log_errors
PARAM_DEFAULT( BoolUserConfigParam(false, "log_errors",
"Enable logging of stdout and stderr to logfile") );
#if defined(WIN32) && !defined(__CYGWIN__)
// No console on windows
# define CONSOLE_DEFAULT false
#else
# define CONSOLE_DEFAULT true
#endif
// No console on windows
PARAM_PREFIX BoolUserConfigParam m_log_errors_to_console
PARAM_DEFAULT( BoolUserConfigParam(
CONSOLE_DEFAULT, "log_errors", "Enable logging to console.") );
PARAM_PREFIX IntUserConfigParam m_reverse_look_threshold
PARAM_DEFAULT( IntUserConfigParam(0, "reverse_look_threshold",

View File

@ -832,16 +832,7 @@ void FileManager::redirectOutput()
Log::verbose("main", "Error messages and other text output will "
"be logged to %s and %s.", logoutfile.c_str(),
logerrfile.c_str());
if(freopen (logoutfile.c_str(),"w",stdout)!=stdout)
{
Log::error("main", "Can not open log file '%s'. Writing to "
"stdout instead.", logoutfile.c_str());
}
if(freopen (logerrfile.c_str(),"w",stderr)!=stderr)
{
Log::error("main", "Can not open log file '%s'. Writing to "
"stderr instead.", logerrfile.c_str());
}
Log::openOutputFiles(logoutfile);
} // redirectOutput
//-----------------------------------------------------------------------------

View File

@ -411,9 +411,9 @@ void cmdLineHelp (char* invocation)
//" --port=n Port number to use.\n"
//" --numclients=n Number of clients to wait for (server "
// "only).\n"
" --log=terminal Write messages to screen.\n"
" --log=file Write messages/warning to log files "
"stdout.log/stderr.log.\n"
" --no-console Does not write messages in the console but to\n"
" stdout.log/stderr.log.\n"
" --console Write messages in the console and files\n"
" -h, --help Show this help.\n"
"\n"
"You can visit SuperTuxKart's homepage at "
@ -480,13 +480,13 @@ int handleCmdLinePreliminary(int argc, char **argv)
UserConfigParams::m_xmas_enabled = false;
}
}
else if( !strcmp(argv[i], "--log=terminal"))
else if( !strcmp(argv[i], "--no-console"))
{
UserConfigParams::m_log_errors=false;
UserConfigParams::m_log_errors_to_console=false;
}
else if( !strcmp(argv[i], "--log=file"))
else if( !strcmp(argv[i], "--console"))
{
UserConfigParams::m_log_errors=true;
UserConfigParams::m_log_errors_to_console=true;
}
else if( !strcmp(argv[i], "--log=nocolor"))
{
@ -1040,10 +1040,10 @@ int handleCmdLine(int argc, char **argv)
else if( !strcmp(argv[i], "--debug=misc" ) ) {}
else if( !strcmp(argv[i], "--debug=all" ) ) {}
else if ( sscanf(argv[i], "--xmas=%d", &n) ) {}
else if( !strcmp(argv[i], "--log=terminal" ) ) {}
else if( !strcmp(argv[i], "--log=nocolor" ) ) {}
else if( !strcmp(argv[i], "--log=file" ) ) {}
else if( sscanf(argv[i], "--log=%d",&n )==1 ) {}
else if( !strcmp(argv[i], "--no-console" ) ) {}
else if( !strcmp(argv[i], "--console" ) ) {}
else if( !strcmp(argv[i], "--screensize") ||
!strcmp(argv[i], "-s") ) {i++;}
else if( !strcmp(argv[i], "--fullscreen") || !strcmp(argv[i], "-f")) {}
@ -1216,8 +1216,9 @@ void cleanSuperTuxKart()
if(stk_config) delete stk_config;
#ifndef WIN32
if (user_config && UserConfigParams::m_log_errors) //close logfiles
if (user_config) //close logfiles
{
Log::closeOutputFiles();
#endif
fclose(stderr);
fclose(stdout);
@ -1282,10 +1283,7 @@ int main(int argc, char *argv[] )
// Windows 32 always redirects output
#ifndef WIN32
if (UserConfigParams::m_log_errors)
{
file_manager->redirectOutput();
}
file_manager->redirectOutput();
#endif
input_manager = new InputManager ();

View File

@ -20,6 +20,7 @@
#include "config/user_config.hpp"
#include <cstdio>
#include <stdio.h>
#ifdef ANDROID
@ -32,6 +33,7 @@
Log::LogLevel Log::m_min_log_level = Log::LL_VERBOSE;
bool Log::m_no_colors = false;
FILE* Log::m_file_stdout = NULL;
// ----------------------------------------------------------------------------
/** Selects background/foreground colors for the message depending on
@ -151,18 +153,58 @@ void Log::printMessage(int level, const char *component, const char *format,
}
__android_log_vprint(alp, "SuperTuxKart", format, va_list);
#else
// If not logged to file, set colours
if(!UserConfigParams::m_log_errors)
// If logged to console, set colours
if(UserConfigParams::m_log_errors_to_console)
setTerminalColor((LogLevel)level);
static const char *names[] = {"verbose", "debug ", "info ",
"warn ", "error ", "fatal "};
printf("[%s] %s: ", names[level], component);
vprintf(format, va_list);
if(!UserConfigParams::m_log_errors)
// If we don't have a console file, write to stdout and hope for the best
if(!m_file_stdout ||
UserConfigParams::m_log_errors_to_console) // log to console & file
{
printf("[%s] %s: ", names[level], component);
vprintf(format, va_list);
printf("\n");
}
if(m_file_stdout)
{
fprintf (m_file_stdout, "[%s] %s: ", names[level], component);
vfprintf(m_file_stdout, format, va_list);
fprintf (m_file_stdout, "\n");
}
if(UserConfigParams::m_log_errors_to_console)
resetTerminalColor();
#endif
} // printMessage
// ----------------------------------------------------------------------------
/** This function opens the files that will contain the output.
* \param logout : name of the file that will contain stdout output
* \param logerr : name of the file that will contain stderr output
*/
void Log::openOutputFiles(const std::string &logout)
{
m_file_stdout = fopen(logout.c_str(), "w");
if (!m_file_stdout)
{
Log::error("main", "Can not open log file '%s'. Writing to "
"stdout instead.", logout);
}
else
{
// Disable buffering so that messages are seen asap
setvbuf(m_file_stdout, NULL, _IONBF, 0);
}
} // closeOutputFiles
// ----------------------------------------------------------------------------
/** Function to close output files */
void Log::closeOutputFiles()
{
fclose(m_file_stdout);
} // closeOutputFiles

View File

@ -21,6 +21,8 @@
#define HEADER_LOG_HPP
#include <stdarg.h>
#include <stdio.h>
#include <string>
#ifdef __GNUC__
# define VALIST __gnuc_va_list
@ -48,6 +50,9 @@ private:
/** If set this will disable coloring of log messages. */
static bool m_no_colors;
/** The file where stdout output will be written */
static FILE* m_file_stdout;
static void setTerminalColor(LogLevel level);
static void resetTerminalColor();
@ -73,6 +78,10 @@ public:
LOG(error, LL_ERROR);
LOG(fatal, LL_FATAL);
static void openOutputFiles(const std::string &logout);
static void closeOutputFiles();
// ------------------------------------------------------------------------
/** Defines the minimum log level to be displayed. */
static void setLogLevel(int n)