OpenVAS Scanner  5.1.3
log.c
Go to the documentation of this file.
1 /* OpenVAS
2 * $Id$
3 * Description: Manages the logfile of OpenVAS.
4 *
5 * Authors: - Renaud Deraison <deraison@nessus.org> (Original pre-fork develoment)
6 * - Tim Brown <mailto:timb@openvas.org> (Initial fork)
7 * - Laban Mwangi <mailto:labanm@openvas.org> (Renaming work)
8 * - Tarik El-Yassem <mailto:tarik@openvas.org> (Headers section)
9 *
10 * Copyright:
11 * Portions Copyright (C) 2006 Software in the Public Interest, Inc.
12 * Based on work Copyright (C) 1998 - 2006 Tenable Network Security, Inc.
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2,
16 * as published by the Free Software Foundation
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
26 */
27 
28 #include <string.h> /* for strchr() */
29 #include <stdio.h> /* for fprintf() */
30 #include <fcntl.h> /* for open() */
31 #include <unistd.h> /* for close() */
32 #include <errno.h> /* for errno() */
33 #include <sys/stat.h> /* for stat() */
34 #include <time.h> /* for time() */
35 
36 #include <stdarg.h>
37 #include <syslog.h>
38 #include "comm.h"
39 #include "utils.h"
40 #include "log.h"
41 
42 static FILE *log = NULL;
43 
47 void
48 log_init (const char *filename)
49 {
50  if ((!filename) || (!strcmp (filename, "stderr")))
51  log = stderr;
52  else if (!strcmp (filename, "syslog"))
53  {
54  openlog ("openvassd", 0, LOG_DAEMON);
55  log = NULL;
56  }
57  else
58  {
59  int fd = open (filename, O_WRONLY | O_CREAT | O_APPEND, 0644);
60  if (fd < 0)
61  {
62  fprintf (stderr, "log_init():open : %s\n", strerror (errno));
63  fprintf (stderr, "Could not open the logfile, using stderr\n");
64  log = stderr;
65  }
66  log = fdopen (fd, "a");
67  if (log == NULL)
68  {
69  perror ("fdopen ");
70  log = stderr;
71  }
72 
73  setlinebuf (log);
74  }
75 }
76 
77 
83 int
85 {
86  return log ? fileno (log) : -1;
87 }
88 
89 void
91 {
92  if (log != NULL)
93  {
94  log_write ("closing logfile");
95  fclose (log);
96  log = NULL;
97  }
98  else
99  closelog ();
100 }
101 
102 
109 void
110 log_vwrite (const char *str, va_list arg_ptr)
111 {
112  char *tmp;
113  char timestr[255];
114  time_t t;
115 
116  if (log == NULL)
117  {
118  vsyslog (LOG_NOTICE, str, arg_ptr);
119  return;
120  }
121 
122  t = time (NULL);
123  tmp = ctime (&t);
124 
125  timestr[sizeof (timestr) - 1] = '\0';
126  strncpy (timestr, tmp, sizeof (timestr) - 1);
127  timestr[strlen (timestr) - 1] = '\0';
128  fprintf (log, "[%s][%d] ", timestr, getpid ());
129  vfprintf (log, str, arg_ptr);
130  fprintf (log, "\n");
131 }
132 
139 void
140 log_write (const char *str, ...)
141 {
142  va_list param;
143 
144  va_start (param, str);
145  log_vwrite (str, param);
146  va_end (param);
147 }
void log_init(const char *filename)
Initialization of the log file.
Definition: log.c:48
void log_write(const char *str,...)
Write into the logfile / syslog.
Definition: log.c:140
int log_get_fd()
Get the open log file descriptor.
Definition: log.c:84
void log_vwrite(const char *str, va_list arg_ptr)
Write into the logfile / syslog using a va_list.
Definition: log.c:110
void log_close()
Definition: log.c:90