/[LeafOK_CVS]/lbbs/src/log.c
ViewVC logotype

Diff of /lbbs/src/log.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.2 by sysadm, Tue Oct 19 02:08:35 2004 UTC Revision 1.15 by sysadm, Mon May 5 11:46:04 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /***************************************************************************
2                            log.c  -  description                                                    log.c  -  description
3                               -------------------                                                           -------------------
4      begin                : Mon Oct 18 2004          begin                : Mon Oct 18 2004
5      copyright            : (C) 2004 by leaf          copyright            : (C) 2004 by leaf
6      email                : leaflet@leafok.com          email                : leaflet@leafok.com
7   ***************************************************************************/   ***************************************************************************/
8    
9  /***************************************************************************  /***************************************************************************
# Line 15  Line 15 
15   *                                                                         *   *                                                                         *
16   ***************************************************************************/   ***************************************************************************/
17    
18  #include "common.h"  #include "io.h"
19    #include <stdio.h>
20    #include <string.h>
21    #include <stdarg.h>
22    #include <sys/types.h>
23    #include <time.h>
24    #include <unistd.h>
25    
26  FILE *fp_log_std;  FILE *fp_log_std;
27  FILE *fp_log_err;  FILE *fp_log_err;
28    
29  int  int log_begin(char *file_log_std, char *file_log_err)
 log_begin (char *file_log_std, char *file_log_err)  
30  {  {
31    fp_log_std = fopen (file_log_std, "a");          fp_log_std = fopen(file_log_std, "a");
32    if (fp_log_std == NULL)          if (fp_log_std == NULL)
33      {          {
34        perror ("log_begin failed\n");                  perror("log_begin failed\n");
35        return -1;                  return -1;
36      }          }
37    
38    fp_log_err = fopen (file_log_err, "a");          fp_log_err = fopen(file_log_err, "a");
39    if (fp_log_err == NULL)          if (fp_log_err == NULL)
40      {          {
41        perror ("log_begin failed\n");                  perror("log_begin failed\n");
42        return -1;                  return -2;
43      }          }
   
   return 0;  
 }  
   
 int  
 log_end ()  
 {  
   fclose (fp_log_std);  
   fclose (fp_log_err);  
 }  
   
 int  
 log_head (char *buf)  
 {  
   time_t t;  
   char s_time[256];  
   t = time(0);  
     
   strftime(s_time,256,"%Y-%m-%d %H:%M:%S", localtime (&t));  
   sprintf(buf,"[%s] [%d] ", s_time, getpid());  
     
   return 0;  
 }  
   
 int  
 log_std (char *msg)  
 {  
   char buf[1024];  
   
   if (fp_log_std == NULL)  
     {  
       perror ("log_std failed\n");  
       return -1;  
     }  
   
   log_head(buf);  
   strcat(buf,msg);  
     
   if (fprintf (fp_log_std, buf)<0)  
     {  
       perror ("log_std failed\n");  
       return -2;  
     }  
   
   fflush(fp_log_std);  
     
   return 0;  
 }  
   
 int  
 log_error (char *error_msg)  
 {  
   char buf[1024];  
     
   if (fp_log_err == NULL)  
     {  
       perror ("log_error failed\n");  
       return -1;  
     }  
   
   log_head(buf);  
   strcat(buf,error_msg);  
   
   if (fprintf (fp_log_err, buf)<0)  
     {  
       perror ("log_error failed\n");  
       return -2;  
     }  
     
   fflush(fp_log_err);  
44    
45    return 0;          return 0;
46    }
47    
48    void log_end()
49    {
50            fclose(fp_log_std);
51            fclose(fp_log_err);
52    }
53    
54    int log_head(char *buf, int len)
55    {
56            time_t t;
57            char s_time[256];
58            t = time(0);
59    
60            strftime(s_time, sizeof(s_time), "%Y-%m-%d %H:%M:%S", localtime(&t));
61            snprintf(buf, len, "[%s] [%d] ", s_time, getpid());
62    
63            return 0;
64    }
65    
66    int log_std(const char *format, ...)
67    {
68            va_list args;
69            int retval;
70            char buf[1024];
71    
72            log_head(buf, sizeof(buf));
73            strncat(buf, format, sizeof(buf) - strnlen(buf, sizeof(buf)));
74    
75            va_start(args, format);
76            retval = vfprintf(fp_log_std, buf, args);
77            va_end(args);
78    
79            fflush(fp_log_std);
80    
81            return retval;
82    }
83    
84    int log_error(const char *format, ...)
85    {
86            va_list args;
87            int retval;
88            char buf[1024];
89    
90            log_head(buf, sizeof(buf));
91            strncat(buf, format, sizeof(buf) - strnlen(buf, sizeof(buf)));
92    
93            va_start(args, format);
94            retval = vfprintf(fp_log_err, buf, args);
95            va_end(args);
96    
97            fflush(fp_log_err);
98    
99            return retval;
100    }
101    
102    int log_std_redirect(int fd)
103    {
104            int ret;
105            close(fileno(fp_log_std));
106            ret = dup2(fd, fileno(fp_log_std));
107            return ret;
108    }
109    
110    int log_err_redirect(int fd)
111    {
112            int ret;
113            close(fileno(fp_log_err));
114            ret = dup2(fd, fileno(fp_log_err));
115            return ret;
116  }  }


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1