00001
00017
00018
00019 #include "msg_log.h"
00020
00021 #include "globals.h"
00022
00023 char *message_prv;
00024 char *message;
00025 short message_hold;
00026 unsigned int message_getcount;
00027 char *msgout_fname;
00028
00035 void message_log_vl(const char *format, va_list val)
00036 {
00037 if (msgout_fname==NULL) return;
00038 FILE *msgout_fp;
00039 msgout_fp=fopen(msgout_fname,"ab");
00040 if (msgout_fp!=NULL)
00041 {
00042
00043 vfprintf(msgout_fp, format, val);
00044 fprintf(msgout_fp,"\r\n");
00045 fclose(msgout_fp);
00046 }
00047 }
00048
00054 void message_log_simp(const char *str)
00055 {
00056 if (msgout_fname==NULL) return;
00057 FILE *msgout_fp;
00058 msgout_fp=fopen(msgout_fname,"ab");
00059
00060 if (msgout_fp!=NULL)
00061 {
00062 fprintf(msgout_fp, "%s\r\n",str);
00063 fclose(msgout_fp);
00064 }
00065 }
00066
00073 void message_log(const char *format, ...)
00074 {
00075 if (msgout_fname==NULL) return;
00076 va_list val;
00077 va_start(val, format);
00078 message_log_vl(format, val);
00079 va_end(val);
00080 }
00081
00088 void message_error(const char *format, ...)
00089 {
00090 va_list val;
00091 va_start(val, format);
00092 char *msg=message_prv;
00093 if (msg==NULL)
00094 {
00095 msg=(char *)malloc(LINEMSG_SIZE*sizeof(char));
00096 if (msg==NULL)
00097 {
00098 fprintf(stderr, "message_error: Cannot allocate memory\n");
00099 return;
00100 }
00101 }
00102 vsprintf(msg, format, val);
00103 va_end(val);
00104
00105 message_log_simp(msg);
00106
00107 message_prv=message;
00108 message=msg;
00109 message_hold=true;
00110 message_getcount=0;
00111 }
00112
00117 short message_is_empty(void)
00118 {
00119 if ((message!=NULL)&&(message[0]>'\0')) return false;
00120 return true;
00121 }
00122
00129 void message_info(const char *format, ...)
00130 {
00131 va_list val;
00132 va_start(val, format);
00133 char *msg=message_prv;
00134 if ((msg==NULL)||(message_hold))
00135 {
00136 msg=(char *)malloc(LINEMSG_SIZE*sizeof(char));
00137 if (msg==NULL)
00138 {
00139 fprintf(stderr, "message_info: Cannot allocate memory\n");
00140 return;
00141 }
00142 }
00143 vsprintf(msg, format, val);
00144 va_end(val);
00145
00146 message_log_simp(msg);
00147 if ((!message_is_empty())&&(message_hold))
00148 {
00149 free(msg);
00150 } else
00151 {
00152 message_prv=message;
00153 message=msg;
00154 message_hold=false;
00155 message_getcount=0;
00156 }
00157 }
00158
00165 void message_info_force(const char *format, ...)
00166 {
00167 va_list val;
00168 va_start(val, format);
00169 char *msg=message_prv;
00170 if (msg==NULL)
00171 {
00172 msg=(char *)malloc(LINEMSG_SIZE*sizeof(char));
00173 if (msg==NULL)
00174 {
00175 fprintf(stderr, "message_info_force: Cannot allocate memory\n");
00176 return;
00177 }
00178 }
00179 vsprintf(msg, format, val);
00180 va_end(val);
00181
00182 message_log_simp(msg);
00183
00184 message_prv=message;
00185 message=msg;
00186 message_hold=false;
00187 message_getcount=0;
00188 }
00189
00196 short message_hold_get(void)
00197 {
00198 return message_hold;
00199 }
00200
00207 unsigned int message_getcount_get(void)
00208 {
00209 return message_getcount;
00210 }
00211
00217 void message_release(void)
00218 {
00219 message_hold=false;
00220 }
00221
00226 char *message_get(void)
00227 {
00228 message_getcount++;
00229 return message;
00230 }
00231
00236 char *message_get_prev(void)
00237 {
00238 return message_prv;
00239 }
00240
00247 short set_msglog_fname(char *fname)
00248 {
00249 if ((fname==NULL)||(fname[0]=='\0'))
00250 {
00251 msgout_fname=NULL;
00252 return false;
00253 }
00254 FILE *msgout_fp;
00255 msgout_fname=strdup(fname);
00256 msgout_fp=fopen(msgout_fname,"wb");
00257 if (msgout_fp!=NULL)
00258 {
00259 fprintf(msgout_fp,"%s message log file\r\n",PROGRAM_NAME);
00260 if (message_prv!=NULL)
00261 fprintf(msgout_fp,"%s\r\n",message_prv);
00262 if (message!=NULL)
00263 fprintf(msgout_fp,"%s\r\n",message);
00264 fclose(msgout_fp);
00265 return true;
00266 }
00267 free(msgout_fname);
00268 msgout_fname=NULL;
00269 return false;
00270 }
00271
00275 void init_messages(void)
00276 {
00277 message=NULL;
00278 message_prv=NULL;
00279 message_hold=false;
00280 message_getcount=0;
00281 msgout_fname=NULL;
00282 }
00283
00287 void free_messages(void)
00288 {
00289 free(message_prv);
00290 free(message);
00291 free(msgout_fname);
00292 }