]>
Commit | Line | Data |
---|---|---|
89b1a15b DD |
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 | ||
b24c88b3 RC |
18 | #if 0 |
19 | static const char *cvsid = | |
20 | "\n%%% $Id$\n"; | |
21 | #endif | |
89b1a15b DD |
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" | |
4a83b7b0 | 35 | #include "mkdir.h" |
a351e48c | 36 | #include "mount.h" |
89b1a15b | 37 | |
3c054baf RC |
38 | #include "io_stream.h" |
39 | ||
b24c88b3 RC |
40 | struct LogEnt |
41 | { | |
89b1a15b | 42 | LogEnt *next; |
3c054baf | 43 | enum log_level level; |
89b1a15b | 44 | time_t when; |
3c054baf | 45 | String msg; |
89b1a15b DD |
46 | }; |
47 | ||
48 | static LogEnt *first_logent = 0; | |
49 | static LogEnt **next_logent = &first_logent; | |
50 | ||
3c054baf RC |
51 | void |
52 | log (enum log_level level, String const &message) | |
89b1a15b | 53 | { |
3c054baf | 54 | LogEnt *l = new LogEnt; |
89b1a15b | 55 | l->next = 0; |
3c054baf | 56 | l->level = level; |
89b1a15b DD |
57 | time (&(l->when)); |
58 | *next_logent = l; | |
59 | next_logent = &(l->next); | |
3c054baf | 60 | l->msg = message; |
89b1a15b | 61 | |
3c054baf RC |
62 | char b[100]; |
63 | b[0]='\0'; | |
64 | if (level == LOG_TIMESTAMP) | |
89b1a15b DD |
65 | { |
66 | struct tm *tm = localtime (&(l->when)); | |
67 | strftime (b, 1000, "%Y/%m/%d %H:%M:%S ", tm); | |
89b1a15b | 68 | } |
3c054baf | 69 | l->msg = String (b) + message; |
89b1a15b | 70 | |
3c054baf RC |
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)); | |
89b1a15b DD |
82 | } |
83 | ||
84 | void | |
3c054baf | 85 | log_save (int babble, String const &filename, int append) |
89b1a15b DD |
86 | { |
87 | static int been_here = 0; | |
88 | if (been_here) | |
89 | return; | |
90 | been_here = 1; | |
91 | ||
3c054baf | 92 | io_stream::mkpath_p (PATH_TO_FILE, filename); |
4a83b7b0 | 93 | |
3c054baf | 94 | io_stream *f = io_stream::open(String("file://")+filename, append ? "at" : "wt"); |
89b1a15b DD |
95 | if (!f) |
96 | { | |
3c054baf | 97 | fatal (NULL, IDS_NOLOGFILE, filename.cstr_oneuse()); |
89b1a15b DD |
98 | return; |
99 | } | |
100 | ||
101 | LogEnt *l; | |
102 | ||
b24c88b3 | 103 | for (l = first_logent; l; l = l->next) |
89b1a15b | 104 | { |
3c054baf | 105 | if (babble || !(l->level == LOG_BABBLE)) |
89b1a15b | 106 | { |
3c054baf RC |
107 | char *tstr = l->msg.cstr(); |
108 | f->write (tstr, strlen (tstr)); | |
109 | if (tstr[strlen (tstr) - 1] != '\n') | |
110 | f->write ("'\n", 1); | |
89b1a15b DD |
111 | } |
112 | } | |
113 | ||
3c054baf | 114 | delete f; |
89b1a15b DD |
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) | |
ab57ceaa | 127 | note (NULL, exit_msg); |
89b1a15b DD |
128 | |
129 | log (LOG_TIMESTAMP, "Ending cygwin install"); | |
130 | ||
3c054baf | 131 | if (source == IDC_SOURCE_DOWNLOAD || !get_root_dir ().size()) |
89b1a15b | 132 | { |
3c054baf RC |
133 | log_save (LOG_BABBLE, local_dir + "/setup.log.full", 0); |
134 | log_save (0, local_dir + "/setup.log", 1); | |
89b1a15b DD |
135 | } |
136 | else | |
137 | { | |
3c054baf RC |
138 | log_save (LOG_BABBLE, cygpath ("/var/log/setup.log.full", 0), 0); |
139 | log_save (0, cygpath ("/var/log/setup.log", 0), 1); | |
89b1a15b DD |
140 | } |
141 | ||
142 | ExitProcess (exit_code); | |
143 | } |