Ignore:
Timestamp:
Sep 21, 2014, 6:03:13 PM (8 years ago)
Author:
jordan
Message:

#4160: mike.dld patch: 4160-06-misc.patch

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/daemon/daemon.c

    r14320 r14335  
    6262#define SPEED_T_STR "TB/s"
    6363
    64 #define LOGFILE_MODE_STR "a+"
    65 
    6664static bool paused = false;
    6765static bool seenHUP = false;
    6866static const char *logfileName = NULL;
    69 static FILE *logfile = NULL;
     67static tr_sys_file_t logfile = TR_BAD_SYS_FILE;
    7068static tr_session * mySession = NULL;
    7169static tr_quark key_pidfile = 0;
     
    145143}
    146144
     145static bool
     146reopen_log_file (const char *filename)
     147{
     148    tr_error * error = NULL;
     149    const tr_sys_file_t old_log_file = logfile;
     150    const tr_sys_file_t new_log_file = tr_sys_file_open (filename,
     151                                                         TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE | TR_SYS_FILE_APPEND,
     152                                                         0666, &error);
     153
     154    if (new_log_file == TR_BAD_SYS_FILE)
     155    {
     156        fprintf (stderr, "Couldn't (re)open log file \"%s\": %s\n", filename, error->message);
     157        tr_error_free (error);
     158        return false;
     159    }
     160
     161    logfile = new_log_file;
     162
     163    if (old_log_file != TR_BAD_SYS_FILE)
     164        tr_sys_file_close (old_log_file, NULL);
     165
     166    return true;
     167}
     168
    147169static void
    148170gotsig (int sig)
     
    163185
    164186                /* reopen the logfile to allow for log rotation */
    165                 if (logfileName) {
    166                     logfile = freopen (logfileName, LOGFILE_MODE_STR, logfile);
    167                     if (!logfile)
    168                         fprintf (stderr, "Couldn't reopen \"%s\": %s\n", logfileName, tr_strerror (errno));
     187                if (logfileName != NULL)
     188                {
     189                    reopen_log_file (logfileName);
    169190                }
    170191
     
    307328
    308329static void
    309 printMessage (FILE * logfile, int level, const char * name, const char * message, const char * file, int line)
    310 {
    311     if (logfile != NULL)
     330printMessage (tr_sys_file_t logfile, int level, const char * name, const char * message, const char * file, int line)
     331{
     332    if (logfile != TR_BAD_SYS_FILE)
    312333    {
    313334        char timestr[64];
    314335        tr_logGetTimeStr (timestr, sizeof (timestr));
    315336        if (name)
    316             fprintf (logfile, "[%s] %s %s (%s:%d)\n", timestr, name, message, file, line);
     337            tr_sys_file_write_fmt (logfile, "[%s] %s %s (%s:%d)" TR_NATIVE_EOL_STR,
     338                                   NULL, timestr, name, message, file, line);
    317339        else
    318             fprintf (logfile, "[%s] %s (%s:%d)\n", timestr, message, file, line);
     340            tr_sys_file_write_fmt (logfile, "[%s] %s (%s:%d)" TR_NATIVE_EOL_STR,
     341                                   NULL, timestr, message, file, line);
    319342    }
    320343#ifdef HAVE_SYSLOG
     
    339362
    340363static void
    341 pumpLogMessages (FILE * logfile)
     364pumpLogMessages (tr_sys_file_t logfile)
    342365{
    343366    const tr_log_message * l;
     
    347370        printMessage (logfile, l->level, l->name, l->message, l->file, l->line);
    348371
    349     if (logfile != NULL)
    350         fflush (logfile);
     372    if (logfile != TR_BAD_SYS_FILE)
     373        tr_sys_file_flush (logfile, NULL);
    351374
    352375    tr_logFreeQueue (list);
     
    440463            case 'd': dumpSettings = true;
    441464                      break;
    442             case 'e': logfile = fopen (optarg, LOGFILE_MODE_STR);
    443                       if (logfile)
     465            case 'e': if (reopen_log_file (optarg))
    444466                          logfileName = optarg;
    445                       else
    446                           fprintf (stderr, "Couldn't open \"%s\": %s\n", optarg, tr_strerror (errno));
    447467                      break;
    448468            case 'f': foreground = true;
     
    519539    }
    520540
    521     if (foreground && !logfile)
    522         logfile = stderr;
     541    if (foreground && logfile == TR_BAD_SYS_FILE)
     542        logfile = tr_sys_file_get_std (TR_STD_SYS_FILE_ERR, NULL);
    523543
    524544    if (!loaded)
     
    569589    if (pid_filename && *pid_filename)
    570590    {
    571         FILE * fp = fopen (pid_filename, "w+");
    572         if (fp != NULL)
     591        tr_error * error = NULL;
     592        tr_sys_file_t fp = tr_sys_file_open (pid_filename,
     593                                             TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE | TR_SYS_FILE_TRUNCATE,
     594                                             0666, &error);
     595        if (fp != TR_BAD_SYS_FILE)
    573596        {
    574             fprintf (fp, "%d", (int)getpid ());
    575             fclose (fp);
     597            tr_sys_file_write_fmt (fp, "%d", NULL, (int)getpid ());
     598            tr_sys_file_close (fp, NULL);
    576599            tr_logAddInfo ("Saved pidfile \"%s\"", pid_filename);
    577600            pidfile_created = true;
    578601        }
    579602        else
    580             tr_logAddError ("Unable to save pidfile \"%s\": %s", pid_filename, tr_strerror (errno));
     603        {
     604            tr_logAddError ("Unable to save pidfile \"%s\": %s", pid_filename, error->message);
     605            tr_error_free (error);
     606        }
    581607    }
    582608
Note: See TracChangeset for help on using the changeset viewer.