| 1 |
/** |
| 2 |
* @file log.c |
| 3 |
* @brief ログ関係 |
| 4 |
* @author BananaJinn |
| 5 |
* @version $Id: log.c,v 1.4 2010/01/17 09:26:25 bananajinn Exp $ |
| 6 |
* 円盤複写屋 |
| 7 |
* Copyright (C) 2004-2006 BananaJinn<banana@mxh.mesh.ne.jp>. |
| 8 |
*/ |
| 9 |
|
| 10 |
#include <stdio.h> |
| 11 |
#include <string.h> |
| 12 |
#include <stdarg.h> |
| 13 |
#if defined(WIN32) |
| 14 |
# include <io.h> |
| 15 |
#else |
| 16 |
# include <pwd.h> |
| 17 |
# include <unistd.h> |
| 18 |
#endif |
| 19 |
#ifdef linux |
| 20 |
# include <fcntl.h> |
| 21 |
#endif |
| 22 |
#include <time.h> |
| 23 |
#include "aspi.h" |
| 24 |
|
| 25 |
#if defined(WIN32) |
| 26 |
# define vsnprintf _vsnprintf |
| 27 |
#endif |
| 28 |
|
| 29 |
#if defined(WIN32) |
| 30 |
# define FILENAME "\\enban-debug.log" |
| 31 |
#else |
| 32 |
# define FILENAME "/enban-debug.log" |
| 33 |
#endif |
| 34 |
|
| 35 |
static char *GetDebugLogPath() |
| 36 |
{ |
| 37 |
static char path[_MAX_PATH]=""; |
| 38 |
#if !defined(WIN32) |
| 39 |
struct passwd *pwent; |
| 40 |
#endif |
| 41 |
|
| 42 |
if(strlen(path)==0){ |
| 43 |
#if defined(WIN32) |
| 44 |
GetCurrentDirectory(sizeof(path), path); |
| 45 |
#else |
| 46 |
pwent = getpwuid(getuid()); |
| 47 |
strncpy(path, pwent->pw_dir, sizeof(path)); |
| 48 |
path[sizeof(path)-1] = '\0'; |
| 49 |
#endif |
| 50 |
if(strlen(path)+strlen(FILENAME)+1 >= sizeof(path)){ |
| 51 |
return NULL; |
| 52 |
} |
| 53 |
strcat(path, FILENAME); |
| 54 |
} |
| 55 |
return path; |
| 56 |
} |
| 57 |
|
| 58 |
static FILE *OpenDebugFile() |
| 59 |
{ |
| 60 |
const char *fname = GetDebugLogPath(); |
| 61 |
if(fname == NULL){ |
| 62 |
return NULL; |
| 63 |
} |
| 64 |
return fopen(fname, "a"); |
| 65 |
} |
| 66 |
|
| 67 |
void DebugLog(char *fmt, ...) |
| 68 |
{ |
| 69 |
#ifdef DEBUGLOG |
| 70 |
va_list args; |
| 71 |
char str[512]; |
| 72 |
time_t now; |
| 73 |
FILE *fp = OpenDebugFile(); |
| 74 |
|
| 75 |
if(fp == NULL){ |
| 76 |
return; |
| 77 |
} |
| 78 |
time(&now); |
| 79 |
strftime(str, sizeof(str)-1, "%Y/%m/%d %H:%M:%S : ", localtime(&now)); |
| 80 |
fputs(str, fp); |
| 81 |
|
| 82 |
va_start(args, fmt); |
| 83 |
vsnprintf(str, sizeof(str), fmt, args); |
| 84 |
|
| 85 |
fputs(str, fp); |
| 86 |
fclose(fp); |
| 87 |
#endif |
| 88 |
} |
| 89 |
|
| 90 |
void DebugDump(void *ptr, size_t size, const char *message) |
| 91 |
{ |
| 92 |
size_t index, index16; |
| 93 |
unsigned char *bytep = (unsigned char *)ptr; |
| 94 |
FILE *fp = OpenDebugFile(); |
| 95 |
|
| 96 |
if(fp == NULL){ |
| 97 |
return; |
| 98 |
} |
| 99 |
|
| 100 |
if(message != NULL){ |
| 101 |
DebugLog("%s\n", message); |
| 102 |
} |
| 103 |
for(index=0; index<size; index++){ |
| 104 |
index16 = index & 15; |
| 105 |
if(index16 == 0){ |
| 106 |
fprintf(fp, "0x%08lX ", (long)(bytep+index)); |
| 107 |
} |
| 108 |
else if(index16 == 8){ |
| 109 |
fputs(" ", fp); |
| 110 |
} |
| 111 |
fprintf(fp, "%02X ", bytep[index]); |
| 112 |
if(index16 == 15){ |
| 113 |
fputs("\n", fp); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
index16 = index & 15; |
| 118 |
if(index16 != 0){ |
| 119 |
fputs("\n", fp); |
| 120 |
} |
| 121 |
fclose(fp); |
| 122 |
} |