]> 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 "mkdir.h"
35 #include "mount.h"
36
37 #include "io_stream.h"
38
39 struct LogEnt
40 {
41 LogEnt *next;
42 enum log_level level;
43 time_t when;
44 String msg;
45 };
46
47 static LogEnt *first_logent = 0;
48 static LogEnt **next_logent = &first_logent;
49
50 void
51 log (enum log_level level, String const &message)
52 {
53 LogEnt *l = new LogEnt;
54 l->next = 0;
55 l->level = level;
56 time (&(l->when));
57 *next_logent = l;
58 next_logent = &(l->next);
59 l->msg = message;
60
61 char b[100];
62 b[0]='\0';
63 if (level == LOG_TIMESTAMP)
64 {
65 struct tm *tm = localtime (&(l->when));
66 strftime (b, 1000, "%Y/%m/%d %H:%M:%S ", tm);
67 }
68 l->msg = String (b) + message;
69
70 msg ("LOG: %d %s", l->level, l->msg.cstr_oneuse());
71 }
72
73 void
74 log (enum log_level level, const char *fmt, ...)
75 {
76 char buf[1000];
77 va_list args;
78 va_start (args, fmt);
79 vsprintf (buf, fmt, args);
80 log (level, String(buf));
81 }
82
83 void
84 log_save (int babble, String const &filename, int append)
85 {
86 static int been_here = 0;
87 if (been_here)
88 return;
89 been_here = 1;
90
91 io_stream::mkpath_p (PATH_TO_FILE, filename);
92
93 io_stream *f = io_stream::open(String("file://")+filename, append ? "at" : "wt");
94 if (!f)
95 {
96 fatal (NULL, IDS_NOLOGFILE, filename.cstr_oneuse());
97 return;
98 }
99
100 LogEnt *l;
101
102 for (l = first_logent; l; l = l->next)
103 {
104 if (babble || !(l->level == LOG_BABBLE))
105 {
106 char *tstr = l->msg.cstr();
107 f->write (tstr, strlen (tstr));
108 if (tstr[strlen (tstr) - 1] != '\n')
109 f->write ("'\n", 1);
110 }
111 }
112
113 delete f;
114 been_here = 0;
115 }
116
117 void
118 exit_setup (int exit_code)
119 {
120 static int been_here = 0;
121 if (been_here)
122 ExitProcess (1);
123 been_here = 1;
124
125 if (exit_msg)
126 note (NULL, exit_msg);
127
128 log (LOG_TIMESTAMP, "Ending cygwin install");
129
130 if (source == IDC_SOURCE_DOWNLOAD || !get_root_dir ().size())
131 {
132 log_save (LOG_BABBLE, local_dir + "/setup.log.full", 0);
133 log_save (0, local_dir + "/setup.log", 1);
134 }
135 else
136 {
137 log_save (LOG_BABBLE, cygpath ("/var/log/setup.log.full"), 0);
138 log_save (0, cygpath ("/var/log/setup.log"), 1);
139 }
140
141 ExitProcess (exit_code);
142 }
This page took 0.038997 seconds and 5 git commands to generate.