1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
|
/* debug.c
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include "srtypes.h"
#include "threadlib.h"
#include "rip_manager.h"
#include "util.h"
#include "debug.h"
#if WIN32
#define vsnprintf _vsnprintf
#endif
/*********************************************************************************
* Public functions
*********************************************************************************/
#define DEBUG_PRINTF_TO_FILE 1
int debug_on = 0;
int command_line_debug = 0;
FILE* gcsfp = 0;
static HSEM m_debug_lock;
static char* debug_filename = 0;
static char filename_buf[SR_MAX_PATH];
static char* default_filename = "gcs.txt";
void
debug_set_filename (char* filename)
{
sr_strncpy (filename_buf, filename, SR_MAX_PATH);
debug_filename = filename_buf;
}
void
debug_enable (void)
{
debug_on = 1;
if (!debug_filename) {
debug_filename = default_filename;
}
}
void
debug_open (void)
{
if (!debug_on) return;
if (!gcsfp) {
gcsfp = fopen(debug_filename, "a");
if (!gcsfp) {
debug_on = 0;
}
}
}
void
debug_close (void)
{
if (!debug_on) return;
if (gcsfp) {
fclose(gcsfp);
gcsfp = 0;
}
}
void
debug_printf (char* fmt, ...)
{
#if (DEBUG_PRINTF_TO_FILE)
static int initialized = 0;
int was_open = 1;
#endif
va_list argptr;
if (!debug_on) return;
if (!initialized) {
m_debug_lock = threadlib_create_sem();
threadlib_signal_sem(&m_debug_lock);
}
threadlib_waitfor_sem (&m_debug_lock);
#if (DEBUG_PRINTF_TO_FILE)
va_start (argptr, fmt);
if (!gcsfp) {
was_open = 0;
debug_open();
if (!gcsfp) return;
}
if (!initialized) {
initialized = 1;
fprintf (gcsfp, "=========================\n");
fprintf (gcsfp, "STREAMRIPPER " SRPLATFORM " " SRVERSION "\n");
}
vfprintf (gcsfp, fmt, argptr);
fflush (gcsfp);
#endif
if (command_line_debug) {
#if (!DEBUG_PRINTF_TO_FILE)
va_start (argptr, fmt);
#endif
vprintf (fmt, argptr);
#if (!DEBUG_PRINTF_TO_FILE)
va_end (argptr);
#endif
}
#if (DEBUG_PRINTF_TO_FILE)
va_end (argptr);
if (!was_open) {
debug_close ();
}
#endif
threadlib_signal_sem (&m_debug_lock);
}
void
debug_print_error (void)
{
#if defined (WIN32)
LPVOID lpMsgBuf;
FormatMessage (
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
debug_printf ("%s", lpMsgBuf);
LocalFree (lpMsgBuf);
#endif
}
|