Exemplo n.º 1
0
  /**
   * Loads config from env vars if present.
   */
  void Init()
  {
    bool shouldAppend = false;
    bool addTimestamp = false;
    bool isSync = false;
    const char* modules = PR_GetEnv("NSPR_LOG_MODULES");
    NSPRLogModulesParser(modules,
        [&shouldAppend, &addTimestamp, &isSync]
            (const char* aName, LogLevel aLevel) mutable {
          if (strcmp(aName, "append") == 0) {
            shouldAppend = true;
          } else if (strcmp(aName, "timestamp") == 0) {
            addTimestamp = true;
          } else if (strcmp(aName, "sync") == 0) {
            isSync = true;
          } else {
            LogModule::Get(aName)->SetLevel(aLevel);
          }
    });

    mAddTimestamp = addTimestamp;
    mIsSync = isSync;

    const char* logFile = PR_GetEnv("NSPR_LOG_FILE");
    if (logFile && logFile[0]) {
      mOutFile = fopen(logFile, shouldAppend ? "a" : "w");
    }
  }
Exemplo n.º 2
0
 /**
  * Loads config from env vars if present.
  */
 void Init()
 {
   const char* modules = PR_GetEnv("NSPR_LOG_MODULES");
   NSPRLogModulesParser(modules, [] (const char* aName, LogLevel aLevel) {
       LogModule::Get(aName)->SetLevel(aLevel);
   });
 }
Exemplo n.º 3
0
    /**
     * Loads config from env vars if present.
     */
    void Init()
    {
        bool shouldAppend = false;
        bool addTimestamp = false;
        bool isSync = false;
        const char* modules = PR_GetEnv("NSPR_LOG_MODULES");
        NSPRLogModulesParser(modules,
                             [&shouldAppend, &addTimestamp, &isSync]
        (const char* aName, LogLevel aLevel) mutable {
            if (strcmp(aName, "append") == 0) {
                shouldAppend = true;
            } else if (strcmp(aName, "timestamp") == 0) {
                addTimestamp = true;
            } else if (strcmp(aName, "sync") == 0) {
                isSync = true;
            } else {
                LogModule::Get(aName)->SetLevel(aLevel);
            }
        });

        mAddTimestamp = addTimestamp;
        mIsSync = isSync;

        const char* logFile = PR_GetEnv("NSPR_LOG_FILE");
        if (logFile && logFile[0]) {
            static const char kPIDToken[] = "%PID";
            const char* pidTokenPtr = strstr(logFile, kPIDToken);
            char buf[2048];
            if (pidTokenPtr &&
                    snprintf_literal(buf, "%.*s%d%s",
                                     static_cast<int>(pidTokenPtr - logFile), logFile,
                                     detail::log_pid(),
                                     pidTokenPtr + strlen(kPIDToken)) > 0)
            {
                logFile = buf;
            }

            mOutFile = fopen(logFile, shouldAppend ? "a" : "w");
        }
    }
Exemplo n.º 4
0
  /**
   * Loads config from env vars if present.
   */
  void Init()
  {
    bool shouldAppend = false;
    bool addTimestamp = false;
    bool isSync = false;
    int32_t rotate = 0;
    const char* modules = PR_GetEnv("MOZ_LOG");
    if (!modules || !modules[0]) {
      modules = PR_GetEnv("MOZ_LOG_MODULES");
      if (modules) {
        NS_WARNING("MOZ_LOG_MODULES is deprecated."
            "\nPlease use MOZ_LOG instead.");
      }
    }
    if (!modules || !modules[0]) {
      modules = PR_GetEnv("NSPR_LOG_MODULES");
      if (modules) {
        NS_WARNING("NSPR_LOG_MODULES is deprecated."
            "\nPlease use MOZ_LOG instead.");
      }
    }

    NSPRLogModulesParser(modules,
        [&shouldAppend, &addTimestamp, &isSync, &rotate]
            (const char* aName, LogLevel aLevel, int32_t aValue) mutable {
          if (strcmp(aName, "append") == 0) {
            shouldAppend = true;
          } else if (strcmp(aName, "timestamp") == 0) {
            addTimestamp = true;
          } else if (strcmp(aName, "sync") == 0) {
            isSync = true;
          } else if (strcmp(aName, "rotate") == 0) {
            rotate = (aValue << 20) / kRotateFilesNumber;
          } else {
            LogModule::Get(aName)->SetLevel(aLevel);
          }
    });

    // Rotate implies timestamp to make the files readable
    mAddTimestamp = addTimestamp || rotate > 0;
    mIsSync = isSync;
    mRotate = rotate;

    if (rotate > 0 && shouldAppend) {
      NS_WARNING("MOZ_LOG: when you rotate the log, you cannot use append!");
    }

    const char* logFile = PR_GetEnv("MOZ_LOG_FILE");
    if (!logFile || !logFile[0]) {
      logFile = PR_GetEnv("NSPR_LOG_FILE");
    }

    if (logFile && logFile[0]) {
      static const char kPIDToken[] = "%PID";
      const char* pidTokenPtr = strstr(logFile, kPIDToken);
      char buf[2048];
      if (pidTokenPtr &&
          SprintfLiteral(buf, "%.*s%d%s",
                         static_cast<int>(pidTokenPtr - logFile), logFile,
                         detail::log_pid(),
                         pidTokenPtr + strlen(kPIDToken)) > 0)
      {
        logFile = buf;
      }

      mOutFilePath.reset(strdup(logFile));

      if (mRotate > 0) {
        // Delete all the previously captured files, including non-rotated
        // log files, so that users don't complain our logs eat space even
        // after the rotate option has been added and don't happen to send
        // us old large logs along with the rotated files.
        remove(mOutFilePath.get());
        for (uint32_t i = 0; i < kRotateFilesNumber; ++i) {
          RemoveFile(i);
        }
      }

      mOutFile = OpenFile(shouldAppend, mOutFileNum);
    }
  }