chiark / gitweb /
modeswitch: morph into tool that only switches Mobile Action cables
[elogind.git] / extras / mobile-action-modeswitch / utils.c
1 /*
2  * Modem mode switcher
3  *
4  * Copyright (C) 2009  Dan Williams <dcbw@redhat.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details:
15  */
16
17 #include <stdarg.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include <time.h>
23
24 #include "utils.h"
25
26 static int debug_on = 0;
27 static int quiet = 0;
28 FILE *logfile = NULL;
29
30 void
31 do_log (int level, const char *fmt, ...)
32 {
33         va_list args;
34         char buffer[1024];
35         char tag = 'L';
36
37         if (level >= LOG_DBG && !debug_on)
38                 return;
39
40         va_start (args, fmt);
41         vsnprintf (buffer, sizeof (buffer), fmt, args);
42         va_end (args);
43
44         if (level == LOG_ERR)
45                 tag = 'E';
46         else if (level == LOG_MSG)
47                 tag = 'L';
48         else if (level == LOG_DBG)
49                 tag = 'D';
50
51         if (logfile)
52                 fprintf (logfile, "%c: %s\n", tag, buffer);
53         if (!quiet)
54                 fprintf ((level == LOG_ERR) ? stderr : stdout, "%c: %s\n", tag, buffer);
55 }
56
57 int
58 log_startup (const char *path, int do_debug, int be_quiet)
59 {
60         time_t t;
61
62         quiet = be_quiet;
63         debug_on = do_debug;
64
65         if (!path)
66                 return 0;
67
68         logfile = fopen (path, "a+");
69         if (!logfile)
70                 return 1;
71
72         t = time (NULL);
73         message ("\n**** Started: %s\n", ctime (&t));
74         return 0;
75 }
76
77 void
78 log_shutdown (void)
79 {
80         if (logfile)
81                 fclose (logfile);
82 }
83