00001
00007 #include "Log.h"
00008
00009
00010 Log* Log::mp_instance = NULL;
00011
00012
00013 Log::Log() {
00014 mp_file = NULL;
00015 mp_filename = NULL;
00016 }
00017
00018 Log::~Log() {
00019 clear();
00020 }
00021
00022 Log* Log::instance() {
00023 if (mp_instance)
00024 return mp_instance;
00025
00026 return mp_instance = new Log();
00027 }
00028
00029 void Log::clear() {
00030 if (mp_instance) delete mp_instance;
00031 mp_instance = NULL;
00032 }
00033
00034 void Log::setFile(char *p_filename) {
00035 if (mp_filename)
00036 delete [] mp_filename;
00037
00038 mp_filename = new char[strlen(p_filename)];
00039 if (!mp_filename)
00040 return;
00041
00042 strcpy(mp_filename, p_filename);
00043
00044 mp_file = fopen(mp_filename, "wb");
00045 if (!mp_file)
00046 return;
00047
00048 fclose(mp_file);
00049
00050 LOG_SUCCESS(("Log file created"));
00051 }
00052
00053 void Log::printError(char *p_text, ...) {
00054 char msg[LOG_MAX_LEN];
00055 va_list vaList;
00056
00057 if (p_text == NULL)
00058 return;
00059
00060 va_start(vaList, p_text);
00061 vsprintf(msg, p_text, vaList);
00062 va_end(vaList);
00063
00064 printFile(LOG_MSG_ERROR, msg);
00065 }
00066
00067 void Log::printSuccess(char *p_text, ...) {
00068 char msg[LOG_MAX_LEN];
00069 va_list vaList;
00070
00071 if (p_text == NULL)
00072 return;
00073
00074 va_start(vaList, p_text);
00075 vsprintf(msg, p_text, vaList);
00076 va_end(vaList);
00077
00078 printFile(LOG_MSG_SUCCESS, msg);
00079 }
00080
00081 void Log::printMisc(char *p_text, ...) {
00082 char msg[LOG_MAX_LEN];
00083 va_list vaList;
00084
00085 if (p_text == NULL)
00086 return;
00087
00088 va_start(vaList, p_text);
00089 vsprintf(msg, p_text, vaList);
00090 va_end(vaList);
00091
00092 printFile(LOG_MSG_MISC, msg);
00093 }
00094
00095 void Log::printFile(int flag, char *msg) {
00096 if (!mp_filename) {
00097 setFile("Log.txt");
00098
00099 if (!mp_filename)
00100 return;
00101 }
00102
00103 mp_file = fopen(mp_filename, "a+");
00104 if (!mp_file)
00105 return;
00106
00107 switch (flag) {
00108 case LOG_MSG_ERROR: fprintf(mp_file, "<!> "); break;
00109 case LOG_MSG_SUCCESS: fprintf(mp_file, "<-> "); break;
00110 case LOG_MSG_MISC: fprintf(mp_file, "<#> "); break;
00111 }
00112
00113 fprintf(mp_file, msg);
00114 putc('\n', mp_file);
00115 fclose(mp_file);
00116 mp_file = NULL;
00117 }
00118
00119 void Log::printNewline() {
00120 if (!mp_filename) {
00121 setFile("Log.txt");
00122
00123 if (!mp_filename)
00124 return;
00125 }
00126
00127 mp_file = fopen(mp_filename, "a+");
00128 if (!mp_file)
00129 return;
00130
00131 putc('\n', mp_file);
00132 fclose(mp_file);
00133 mp_file = NULL;
00134 }