Update usage and man page for ao-postflight
[fw/altos] / ao-tools / lib / cc-log.c
1 /*
2  * Copyright © 2009 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include <string.h>
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <unistd.h>
22 #include <gconf/gconf-client.h>
23 #include "cc.h"
24
25 static char *cc_file_dir;
26
27 #define ALTOS_DIR_PATH  "/apps/aoview/log_dir"
28 #define DEFAULT_DIR     "AltOS"
29
30 static void
31 cc_file_save_conf(void)
32 {
33         GConfClient     *gconf_client;
34
35         g_type_init();
36         gconf_client = gconf_client_get_default();
37         if (gconf_client)
38         {
39                 gconf_client_set_string(gconf_client,
40                                         ALTOS_DIR_PATH,
41                                         cc_file_dir,
42                                         NULL);
43                 g_object_unref(G_OBJECT(gconf_client));
44         }
45 }
46
47 static void
48 cc_file_load_conf(void)
49 {
50         char *file_dir;
51         GConfClient     *gconf_client;
52
53         g_type_init();
54         gconf_client = gconf_client_get_default();
55         if (gconf_client)
56         {
57                 file_dir = gconf_client_get_string(gconf_client,
58                                                    ALTOS_DIR_PATH,
59                                                    NULL);
60                 g_object_unref(G_OBJECT(gconf_client));
61                 if (file_dir)
62                         cc_file_dir = strdup(file_dir);
63         }
64 }
65
66 void
67 cc_set_log_dir(char *dir)
68 {
69         cc_file_dir = strdup(dir);
70         cc_file_save_conf();
71 }
72
73 char *
74 cc_get_log_dir(void)
75 {
76         cc_file_load_conf();
77         if (!cc_file_dir) {
78                 cc_file_dir = cc_fullname(getenv("HOME"), DEFAULT_DIR);
79                 cc_file_save_conf();
80         }
81         return cc_file_dir;
82 }
83
84 char *
85 cc_make_filename(int serial, char *ext)
86 {
87         char            base[50];
88         struct tm       tm;
89         time_t          now;
90         char            *full;
91         int             r;
92         int             sequence;
93
94         now = time(NULL);
95         (void) localtime_r(&now, &tm);
96         cc_mkdir(cc_get_log_dir());
97         sequence = 0;
98         for (;;) {
99                 snprintf(base, sizeof (base), "%04d-%02d-%02d-serial-%03d-flight-%03d.%s",
100                         tm.tm_year + 1900,
101                         tm.tm_mon + 1,
102                         tm.tm_mday,
103                         serial,
104                         sequence,
105                         ext);
106                 full = cc_fullname(cc_get_log_dir(), base);
107                 r = access(full, F_OK);
108                 if (r < 0)
109                         return full;
110                 free(full);
111                 sequence++;
112         }
113
114 }