| 1 |
/** |
| 2 |
* Different ways to give information to the user |
| 3 |
|
| 4 |
* Copyright (C) 1997, 1998, 1999, 2002, 2003 Seth A. Robinson |
| 5 |
* Copyright (C) 2007, 2008, 2009, 2014 Sylvain Beucler |
| 6 |
|
| 7 |
* This file is part of GNU FreeDink |
| 8 |
|
| 9 |
* GNU FreeDink is free software; you can redistribute it and/or |
| 10 |
* modify it under the terms of the GNU General Public License as |
| 11 |
* published by the Free Software Foundation; either version 3 of the |
| 12 |
* License, or (at your option) any later version. |
| 13 |
|
| 14 |
* GNU FreeDink is distributed in the hope that it will be useful, but |
| 15 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 |
* General Public License for more details. |
| 18 |
|
| 19 |
* You should have received a copy of the GNU General Public License |
| 20 |
* along with this program. If not, see |
| 21 |
* <http://www.gnu.org/licenses/>. |
| 22 |
*/ |
| 23 |
|
| 24 |
#ifdef HAVE_CONFIG_H |
| 25 |
#include <config.h> |
| 26 |
#endif |
| 27 |
|
| 28 |
#include <string.h> /* strerror */ |
| 29 |
#include <errno.h> |
| 30 |
#include "log.h" |
| 31 |
|
| 32 |
#include <string> |
| 33 |
using namespace std; |
| 34 |
|
| 35 |
int debug_mode = 0; |
| 36 |
static string lastLog; |
| 37 |
static SDL_LogOutputFunction sdlLogger; |
| 38 |
static string filename; |
| 39 |
static FILE* out = NULL; |
| 40 |
|
| 41 |
static char* init_error_msg = NULL; |
| 42 |
|
| 43 |
void log_output(void *userdata, |
| 44 |
int category, SDL_LogPriority priority, |
| 45 |
const char *message) { |
| 46 |
sdlLogger(userdata, category, priority, message); |
| 47 |
lastLog = message; |
| 48 |
|
| 49 |
if (debug_mode) { |
| 50 |
// display message on screen in debug mode |
| 51 |
// also write to DEBUG.TXT |
| 52 |
if (out != NULL) { |
| 53 |
fputs(message, out); |
| 54 |
fputc('\n', out); |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
void log_init() { |
| 60 |
/* Decorate default SDL log */ |
| 61 |
SDL_LogGetOutputFunction(&sdlLogger, NULL); |
| 62 |
SDL_LogSetOutputFunction(log_output, NULL); |
| 63 |
} |
| 64 |
|
| 65 |
void log_quit() { |
| 66 |
SDL_LogSetOutputFunction(sdlLogger, NULL); |
| 67 |
if (init_error_msg != NULL) |
| 68 |
free(init_error_msg); |
| 69 |
} |
| 70 |
|
| 71 |
void log_debug_on() { |
| 72 |
debug_mode = 1; |
| 73 |
SDL_LogSetAllPriority(SDL_LOG_PRIORITY_DEBUG); |
| 74 |
|
| 75 |
out = fopen(filename.c_str(), "a"); |
| 76 |
if (out == NULL) { |
| 77 |
log_error("Cannot open %s: %s", filename.c_str(), strerror(errno)); |
| 78 |
return; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
void log_debug_off() |
| 83 |
{ |
| 84 |
lastLog = ""; |
| 85 |
if (out != NULL) |
| 86 |
fclose(out); |
| 87 |
out = NULL; |
| 88 |
debug_mode = 0; |
| 89 |
SDL_LogResetPriorities(); |
| 90 |
SDL_LogSetAllPriority(SDL_LOG_PRIORITY_ERROR); |
| 91 |
/* Avoid startup error about X11 missing symbols */ |
| 92 |
SDL_LogSetPriority(SDL_LOG_CATEGORY_ERROR, SDL_LOG_PRIORITY_CRITICAL); |
| 93 |
/* If you need to debug early: */ |
| 94 |
/* SDL_LogSetAllPriority(SDL_LOG_PRIORITY_DEBUG); */ |
| 95 |
} |
| 96 |
|
| 97 |
const char* log_getLastLog() |
| 98 |
{ |
| 99 |
return lastLog.c_str(); |
| 100 |
} |
| 101 |
|
| 102 |
void log_set_output_file(const char* fn) { |
| 103 |
filename = fn; |
| 104 |
} |