/[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.5 by sysadm, Thu Oct 21 17:28:46 2004 UTC Revision 1.13 by sysadm, Sun May 4 14:54:55 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 -2;                  return -2;
43      }          }
44    
45    return 0;          return 0;
46  }  }
47    
48  int  void log_end()
 log_end ()  
49  {  {
50    fclose (fp_log_std);          fclose(fp_log_std);
51    fclose (fp_log_err);          fclose(fp_log_err);
52  }  }
53    
54  int  int log_head(char *buf)
 log_head (char *buf)  
55  {  {
56    time_t t;          time_t t;
57    char s_time[256];          char s_time[256];
58    t = time(0);          t = time(0);
59      
60    strftime(s_time,256,"%Y-%m-%d %H:%M:%S", localtime (&t));          strftime(s_time, 256, "%Y-%m-%d %H:%M:%S", localtime(&t));
61    sprintf(buf,"[%s] [%d] ", s_time, getpid());          sprintf(buf, "[%s] [%d] ", s_time, getpid());
62      
63    return 0;          return 0;
64  }  }
65    
66  int  int log_std(const char *format, ...)
 log_std (char *msg)  
67  {  {
68    char buf[1024];          va_list args;
69            int retval;
70            char buf[1024];
71    
72    if (fp_log_std == NULL)          log_head(buf);
73      {          strcat(buf, format);
       perror ("log_std failed\n");  
       return -1;  
     }  
74    
75    log_head(buf);          va_start(args, format);
76            retval = vfprintf(fp_log_std, buf, args);
77            va_end(args);
78    
79    strcat(buf,msg);          fflush(fp_log_std);
     
   if (fprintf (fp_log_std, buf)<0)  
     {  
       perror ("log_std failed\n");  
       return -2;  
     }  
80    
81    fflush(fp_log_std);          return retval;
     
   return 0;  
82  }  }
83    
84  int  int log_error(const char *format, ...)
 log_error (char *error_msg)  
85  {  {
86    char buf[1024];          va_list args;
87              int retval;
88    if (fp_log_err == NULL)          char buf[1024];
     {  
       perror ("log_error failed\n");  
       return -1;  
     }  
89    
90    log_head(buf);          log_head(buf);
91            strcat(buf, format);
92    
93    strcat(buf,error_msg);          va_start(args, format);
94            retval = vfprintf(fp_log_err, buf, args);
95            va_end(args);
96    
97    if (fprintf (fp_log_err, buf)<0)          fflush(fp_log_err);
     {  
       perror ("log_error failed\n");  
       return -2;  
     }  
     
   fflush(fp_log_err);  
98    
99    return 0;          return retval;
100  }  }
101    
102  int  int log_std_redirect(int fd)
 log_std_redirect(int fd)  
103  {  {
104    int ret;          int ret;
105    close (fileno(fp_log_std));          close(fileno(fp_log_std));
106    ret =  dup2(fd, fileno(fp_log_std));          ret = dup2(fd, fileno(fp_log_std));
107    return ret;          return ret;
108  }  }
109    
110  int  int log_err_redirect(int fd)
 log_err_redirect(int fd)  
111  {  {
112    int ret;          int ret;
113    close (fileno(fp_log_err));          close(fileno(fp_log_err));
114    ret =  dup2(fd, fileno(fp_log_err));          ret = dup2(fd, fileno(fp_log_err));
115    return ret;          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