]> cygwin.com Git - cygwin-apps/setup.git/blob - log.cc
2002-02-19 Robert Collins <rbtcollins@hotmail.com>
[cygwin-apps/setup.git] / log.cc
1 /*
2 * Copyright (c) 2000, Red Hat, Inc.
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; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * A copy of the GNU General Public License can be found at
10 * http://www.gnu.org/
11 *
12 * Written by DJ Delorie <dj@redhat.com>
13 *
14 */
15
16 /* The purpose of this file is to centralize all the logging functions. */
17
18 #if 0
19 static const char *cvsid =
20 "\n%%% $Id$\n";
21 #endif
22
23 #include "win32.h"
24 #include <stdio.h>
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #include <time.h>
28
29 #include "resource.h"
30 #include "msg.h"
31 #include "log.h"
32 #include "dialog.h"
33 #include "state.h"
34 #include "concat.h"
35 #include "mkdir.h"
36 #include "mount.h"
37
38 #include "io_stream.h"
39
40 struct LogEnt
41 {
42 LogEnt *next;
43 enum log_level level;
44 time_t when;
45 String msg;
46 };
47
48 static LogEnt *first_logent = 0;
49 static LogEnt **next_logent = &first_logent;
50
51 void
52 log (enum log_level level, String const &message)
53 {
54 LogEnt *l = new LogEnt;
55 l->next = 0;
56 l->level = level;
57 time (&(l->when));
58 *next_logent = l;
59 next_logent = &(l->next);
60 l->msg = message;
61
62 char b[100];
63 b[0]='\0';
64 if (level == LOG_TIMESTAMP)
65 {
66 struct tm *tm = localtime (&(l->when));
67 strftime (b, 1000, "%Y/%m/%d %H:%M:%S ", tm);
68 }
69 l->msg = String (b) + message;
70
71 msg ("LOG: %d %s", l->level, l->msg.cstr_oneuse());
72 }
73
74 void
75 log (enum log_level level, const char *fmt, ...)
76 {
77 char buf[1000];
78 va_list args;
79 va_start (args, fmt);
80 vsprintf (buf, fmt, args);
81 log (level, String(buf));
82 }
83
84 void
85 log_save (int babble, String const &filename, int append)
86 {
87 static int been_here = 0;
88 if (been_here)
89 return;
90 been_here = 1;
91
92 io_stream::mkpath_p (PATH_TO_FILE, filename);
93
94 io_stream *f = io_stream::open(String("file://")+filename, append ? "at" : "wt");
95 if (!f)
96 {
97 fatal (NULL, IDS_NOLOGFILE, filename.cstr_oneuse());
98 return;
99 }
100
101 LogEnt *l;
102
103 for (l = first_logent; l; l = l->next)
104 {
105 if (babble || !(l->level == LOG_BABBLE))
106 {
107 char *tstr = l->msg.cstr();
108 f->write (tstr, strlen (tstr));
109 if (tstr[strlen (tstr) - 1] != '\n')
110 f->write ("'\n", 1);
111 }
112 }
113
114 delete f;
115 been_here = 0;
116 }
117
118 void
119 exit_setup (int exit_code)
120 {
121 static int been_here = 0;
122 if (been_here)
123 ExitProcess (1);
124 been_here = 1;
125
126 if (exit_msg)
127 note (NULL, exit_msg);
128
129 log (LOG_TIMESTAMP, "Ending cygwin install");
130
131 if (source == IDC_SOURCE_DOWNLOAD || !get_root_dir ().size())
132 {
133 log_save (LOG_BABBLE, local_dir + "/setup.log.full", 0);
134 log_save (0, local_dir + "/setup.log", 1);
135 }
136 else
137 {
138 log_save (LOG_BABBLE, cygpath ("/var/log/setup.log.full", 0), 0);
139 log_save (0, cygpath ("/var/log/setup.log", 0), 1);
140 }
141
142 ExitProcess (exit_code);
143 }
This page took 0.039507 seconds and 5 git commands to generate.