1 /**
2  * log.c -- log and error handling.
3  *    _______       _______            __        __
4  *   / ____/ |     / / ___/____  _____/ /_____  / /_
5  *  / / __ | | /| / /\__ \/ __ \/ ___/ //_/ _ \/ __/
6  * / /_/ / | |/ |/ /___/ / /_/ / /__/ ,< /  __/ /_
7  * \____/  |__/|__//____/\____/\___/_/|_|\___/\__/
8  *
9  * The MIT License (MIT)
10  * Copyright (c) 2009-2016 Gerardo Orellana <hello @ goaccess.io>
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy
13  * of this software and associated documentation files (the "Software"), to deal
14  * in the Software without restriction, including without limitation the rights
15  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16  * copies of the Software, and to permit persons to whom the Software is
17  * furnished to do so, subject to the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be included in all
20  * copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28  * SOFTWARE.
29  */
30 
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdarg.h>
34 #include <unistd.h>
35 
36 #include "log.h"
37 #include "xmalloc.h"
38 
39 static FILE *log_file;
40 
41 /* Open a access file whose name is specified in the given path. */
42 int
access_log_open(const char * path)43 access_log_open (const char *path)
44 {
45   if (path == NULL)
46     return 0;
47 
48   if (access (path, F_OK) != -1)
49     log_file = fopen (path, "a");
50   else
51     log_file = fopen (path, "w");
52   if (log_file == NULL)
53     return 1;
54 
55   return 0;
56 }
57 
58 /* Close the access log file. */
59 void
access_log_close(void)60 access_log_close (void)
61 {
62   if (log_file != NULL)
63     fclose (log_file);
64 }
65 
66 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
67 /* Debug otuput */
68 void
dbg_printf(const char * fmt,...)69 dbg_printf (const char *fmt, ...)
70 {
71   va_list args;
72   va_start (args, fmt);
73   vfprintf (stderr, fmt, args);
74   va_end (args);
75 }
76 
77 /* Write formatted access log data to the logfile. */
78 void
access_fprintf(const char * fmt,...)79 access_fprintf (const char *fmt, ...)
80 {
81   va_list args;
82 
83   if (!log_file)
84     return;
85 
86   va_start (args, fmt);
87   vfprintf (log_file, fmt, args);
88   fflush (log_file);
89   va_end (args);
90 }
91 
92 #pragma GCC diagnostic warning "-Wformat-nonliteral"
93