1 | Index: main.cpp |
---|
2 | =================================================================== |
---|
3 | --- main.cpp (revision 15218) |
---|
4 | +++ main.cpp (working copy) |
---|
5 | @@ -1,5 +1,9 @@ |
---|
6 | #include <qapplication.h> |
---|
7 | #include <cstdlib> |
---|
8 | +#include <signal.h> |
---|
9 | +#include <sys/types.h> |
---|
10 | +#include <sys/stat.h> |
---|
11 | +#include <fcntl.h> |
---|
12 | |
---|
13 | #include "libmyth/mythcontext.h" |
---|
14 | #include "libmyth/settings.h" |
---|
15 | @@ -12,6 +16,13 @@ |
---|
16 | #include "welcomedialog.h" |
---|
17 | #include "welcomesettings.h" |
---|
18 | |
---|
19 | + |
---|
20 | +QString logfile = ""; |
---|
21 | + |
---|
22 | +static bool log_rotate(bool report_error); |
---|
23 | +static void log_rotate_handler(int); |
---|
24 | + |
---|
25 | + |
---|
26 | void initKeys(void) |
---|
27 | { |
---|
28 | REG_KEY("Welcome", "STARTXTERM", "Open an Xterm window", "F12"); |
---|
29 | @@ -67,16 +78,47 @@ |
---|
30 | { |
---|
31 | bShowSettings = true; |
---|
32 | } |
---|
33 | + else if (!strcmp(a.argv()[argpos], "-l") || |
---|
34 | + !strcmp(a.argv()[argpos], "--logfile")) |
---|
35 | + { |
---|
36 | + if (a.argc()-1 > argpos) |
---|
37 | + { |
---|
38 | + logfile = a.argv()[argpos+1]; |
---|
39 | + if (logfile.startsWith("-")) |
---|
40 | + { |
---|
41 | + cerr << "Invalid or missing argument to -l/--logfile option\n"; |
---|
42 | + return FRONTEND_EXIT_INVALID_CMDLINE; |
---|
43 | + } |
---|
44 | + else |
---|
45 | + { |
---|
46 | + ++argpos; |
---|
47 | + } |
---|
48 | + } |
---|
49 | + else |
---|
50 | + { |
---|
51 | + cerr << "Missing argument to -l/--logfile option\n"; |
---|
52 | + return FRONTEND_EXIT_INVALID_CMDLINE; |
---|
53 | + } |
---|
54 | + } |
---|
55 | else |
---|
56 | { |
---|
57 | cerr << "Invalid argument: " << a.argv()[argpos] << endl << |
---|
58 | "Valid options are: " << endl << |
---|
59 | "-v or --verbose debug-level Use '-v help' for level info" << endl << |
---|
60 | - "-s or --setup Run setup for the mythshutdown program" << endl; |
---|
61 | + "-s or --setup Run setup for the mythshutdown program" << endl << |
---|
62 | + "-l or --logfile filename Writes STDERR and STDOUT messages to filename" << endl; |
---|
63 | return FRONTEND_EXIT_INVALID_CMDLINE; |
---|
64 | } |
---|
65 | } |
---|
66 | |
---|
67 | + if (logfile != "") |
---|
68 | + { |
---|
69 | + if (!log_rotate(true)) |
---|
70 | + cerr << "cannot open logfile; using stdout/stderr" << endl; |
---|
71 | + else |
---|
72 | + signal(SIGHUP, &log_rotate_handler); |
---|
73 | + } |
---|
74 | + |
---|
75 | LanguageSettings::load("mythfrontend"); |
---|
76 | |
---|
77 | gContext->LoadQtConfig(); |
---|
78 | @@ -105,3 +147,39 @@ |
---|
79 | |
---|
80 | return 0; |
---|
81 | } |
---|
82 | + |
---|
83 | + |
---|
84 | +static bool log_rotate(bool report_error) |
---|
85 | +{ |
---|
86 | + int new_logfd = open(logfile, O_WRONLY|O_CREAT|O_APPEND, 0664); |
---|
87 | + |
---|
88 | + if (new_logfd < 0) |
---|
89 | + { |
---|
90 | + /* If we can't open the new logfile, send data to /dev/null */ |
---|
91 | + if (report_error) |
---|
92 | + { |
---|
93 | + cerr << "cannot open logfile " << logfile << endl; |
---|
94 | + return false; |
---|
95 | + } |
---|
96 | + |
---|
97 | + new_logfd = open("/dev/null", O_WRONLY); |
---|
98 | + |
---|
99 | + if (new_logfd < 0) |
---|
100 | + { |
---|
101 | + /* There's not much we can do, so punt. */ |
---|
102 | + return false; |
---|
103 | + } |
---|
104 | + } |
---|
105 | + |
---|
106 | + while (dup2(new_logfd, 1) < 0 && errno == EINTR); |
---|
107 | + while (dup2(new_logfd, 2) < 0 && errno == EINTR); |
---|
108 | + while (close(new_logfd) < 0 && errno == EINTR); |
---|
109 | + |
---|
110 | + return true; |
---|
111 | +} |
---|
112 | + |
---|
113 | + |
---|
114 | +static void log_rotate_handler(int) |
---|
115 | +{ |
---|
116 | + log_rotate(false); |
---|
117 | +} |
---|