toolkit/mozapps/update/common/updatelogging.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/mozapps/update/common/updatelogging.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,82 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#if defined(XP_WIN)
     1.9 +#include <windows.h>
    1.10 +#endif
    1.11 +
    1.12 +
    1.13 +#include <stdio.h>
    1.14 +#include <string.h>
    1.15 +#include <stdlib.h>
    1.16 +#include <stdarg.h>
    1.17 +
    1.18 +#include "updatelogging.h"
    1.19 +
    1.20 +UpdateLog::UpdateLog() : logFP(nullptr)
    1.21 +{
    1.22 +}
    1.23 +
    1.24 +void UpdateLog::Init(NS_tchar* sourcePath,
    1.25 +                     const NS_tchar* fileName,
    1.26 +                     const NS_tchar* alternateFileName,
    1.27 +                     bool append)
    1.28 +{
    1.29 +  if (logFP)
    1.30 +    return;
    1.31 +
    1.32 +  this->sourcePath = sourcePath;
    1.33 +  NS_tchar logFile[MAXPATHLEN];
    1.34 +  NS_tsnprintf(logFile, sizeof(logFile)/sizeof(logFile[0]),
    1.35 +    NS_T("%s/%s"), sourcePath, fileName);
    1.36 +
    1.37 +  if (alternateFileName && NS_taccess(logFile, F_OK)) {
    1.38 +    NS_tsnprintf(logFile, sizeof(logFile)/sizeof(logFile[0]),
    1.39 +      NS_T("%s/%s"), sourcePath, alternateFileName);
    1.40 +  }
    1.41 +
    1.42 +  logFP = NS_tfopen(logFile, append ? NS_T("a") : NS_T("w"));
    1.43 +}
    1.44 +
    1.45 +void UpdateLog::Finish()
    1.46 +{
    1.47 +  if (!logFP)
    1.48 +    return;
    1.49 +
    1.50 +  fclose(logFP);
    1.51 +  logFP = nullptr;
    1.52 +}
    1.53 +
    1.54 +void UpdateLog::Flush()
    1.55 +{
    1.56 +  if (!logFP)
    1.57 +    return;
    1.58 +
    1.59 +  fflush(logFP);
    1.60 +}
    1.61 +
    1.62 +void UpdateLog::Printf(const char *fmt, ... )
    1.63 +{
    1.64 +  if (!logFP)
    1.65 +    return;
    1.66 +
    1.67 +  va_list ap;
    1.68 +  va_start(ap, fmt);
    1.69 +  vfprintf(logFP, fmt, ap);
    1.70 +  fprintf(logFP, "\n");
    1.71 +  va_end(ap);
    1.72 +}
    1.73 +
    1.74 +void UpdateLog::WarnPrintf(const char *fmt, ... )
    1.75 +{
    1.76 +  if (!logFP)
    1.77 +    return;
    1.78 +
    1.79 +  va_list ap;
    1.80 +  va_start(ap, fmt);
    1.81 +  fprintf(logFP, "*** Warning: ");
    1.82 +  vfprintf(logFP, fmt, ap);
    1.83 +  fprintf(logFP, "***\n");
    1.84 +  va_end(ap);
    1.85 +}

mercurial