/[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.4 by sysadm, Tue Oct 19 17:11:39 2004 UTC Revision 1.16 by sysadm, Mon May 5 14:27:57 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");
39            if (fp_log_err == NULL)
40            {
41                    perror("log_begin failed\n");
42                    return -2;
43            }
44    
45    fp_log_err = fopen (file_log_err, "a");          return 0;
   if (fp_log_err == NULL)  
     {  
       perror ("log_begin failed\n");  
       return -2;  
     }  
   
   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, size_t len)
 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, sizeof(s_time), "%Y-%m-%d %H:%M:%S", localtime(&t));
61    sprintf(buf,"[%s] [%d] ", s_time, getpid());          snprintf(buf, len, "[%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, sizeof(buf));
73      {          strncat(buf, format, sizeof(buf) - strnlen(buf, sizeof(buf)));
       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, sizeof(buf));
91            strncat(buf, format, sizeof(buf) - strnlen(buf, sizeof(buf)));
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);
98      {  
99        perror ("log_error failed\n");          return retval;
100        return -2;  }
     }  
     
   fflush(fp_log_err);  
101    
102    return 0;  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