Ticket #6945: 6945-mythtvsetup-logfile-support.diff
File 6945-mythtvsetup-logfile-support.diff, 4.1 KB (added by , 15 years ago) |
---|
-
mythtv/programs/mythtv-setup/main.cpp
1 2 #include <unistd.h> 3 #include <fcntl.h> 4 1 5 #include <iostream> 2 6 #include <memory> 3 7 … … 5 9 #include <QDir> 6 10 #include <QMap> 7 11 #include <QApplication> 12 #include <QFileInfo> 8 13 9 14 #include "mythconfig.h" 10 15 #include "mythcontext.h" … … 37 42 ExitPrompter *exitPrompt = NULL; 38 43 StartPrompter *startPrompt = NULL; 39 44 45 static QString logfile; 46 40 47 void SetupMenuCallback(void* data, QString& selection) 41 48 { 42 49 (void)data; … … 126 133 "Force the setting named 'KEY' to value 'VALUE'" << endl 127 134 << "-v or --verbose debug-level " 128 135 "Use '-v help' for level info" << endl 136 << "-l or --logfile filename " 137 "Writes STDERR and STDOUT messages to filename" << endl 129 138 << endl; 130 139 } 131 140 141 int log_rotate(int report_error) 142 { 143 int new_logfd = open(logfile.toLocal8Bit().constData(), 144 O_WRONLY|O_CREAT|O_APPEND, 0664); 145 146 if (new_logfd < 0) { 147 /* If we can't open the new logfile, send data to /dev/null */ 148 if (report_error) 149 { 150 VERBOSE(VB_IMPORTANT, QString("Can not open logfile '%1'") 151 .arg(logfile)); 152 return -1; 153 } 154 155 new_logfd = open("/dev/null", O_WRONLY); 156 157 if (new_logfd < 0) { 158 /* There's not much we can do, so punt. */ 159 return -1; 160 } 161 } 162 163 while (dup2(new_logfd, 1) < 0 && errno == EINTR); 164 while (dup2(new_logfd, 2) < 0 && errno == EINTR); 165 while (close(new_logfd) < 0 && errno == EINTR); 166 167 return 0; 168 } 169 170 void log_rotate_handler(int) 171 { 172 log_rotate(0); 173 } 174 132 175 int main(int argc, char *argv[]) 133 176 { 134 177 QString geometry = QString::null; … … 177 220 178 221 QMap<QString, QString> settingsOverride; 179 222 223 QFileInfo finfo(a.argv()[0]); 224 225 QString binname = finfo.baseName(); 226 227 extern const char *myth_source_version; 228 extern const char *myth_source_path; 229 230 VERBOSE(VB_IMPORTANT, QString("%1 version: %2 [%3] www.mythtv.org") 231 .arg(binname) 232 .arg(myth_source_path) 233 .arg(myth_source_version)); 234 235 180 236 for(int argpos = 1; argpos < a.argc(); ++argpos) 181 237 { 182 238 if (!strcmp(a.argv()[argpos],"-display") || … … 220 276 return BACKEND_EXIT_INVALID_CMDLINE; 221 277 } 222 278 } 279 else if (!strcmp(a.argv()[argpos],"-l") || 280 !strcmp(a.argv()[argpos],"--logfile")) 281 { 282 if (a.argc()-1 > argpos) 283 { 284 logfile = a.argv()[argpos+1]; 285 if (logfile.startsWith("-")) 286 { 287 cerr << "Invalid or missing argument" 288 " to -l/--logfile option\n"; 289 return BACKEND_EXIT_INVALID_CMDLINE; 290 } 291 else 292 { 293 ++argpos; 294 } 295 } 296 else 297 { 298 cerr << "Missing argument to -l/--logfile option\n"; 299 return BACKEND_EXIT_INVALID_CMDLINE; 300 } 301 } 223 302 else if (!strcmp(a.argv()[argpos],"-v") || 224 303 !strcmp(a.argv()[argpos],"--verbose")) 225 304 { … … 324 403 } 325 404 } 326 405 406 if (logfile.size()) 407 { 408 if (log_rotate(1) < 0) 409 cerr << "cannot open logfile; using stdout/stderr" << endl; 410 else 411 { 412 VERBOSE(VB_IMPORTANT, QString("%1 version: %2 [%3] www.mythtv.org") 413 .arg(binname) 414 .arg(myth_source_path) 415 .arg(myth_source_version)); 416 417 signal(SIGHUP, &log_rotate_handler); 418 } 419 } 420 327 421 if (!display.isEmpty()) 328 422 { 329 423 MythUIHelper::SetX11Display(display);