tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

logger.c (2865B)


      1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 /*
      7 * File:        logger.c
      8 * Description: test program for logging's basic functions
      9 */
     10 
     11 #include "prinit.h"
     12 #include "prlog.h"
     13 #include "prlock.h"
     14 #include "prcvar.h"
     15 #include "prthread.h"
     16 #include "prinrval.h"
     17 
     18 #include <stdio.h>
     19 
     20 /* lth. re-define PR_LOG() */
     21 #if 0
     22 #  undef PR_LOG_TEST
     23 #  undef PR_LOG
     24 #  define PR_LOG_TEST(_module, _level) ((_module)->level <= (_level))
     25 #  define PR_LOG(_module, _level, _args)                   \
     26    {                                                      \
     27      if (PR_LOG_TEST(_module, _level)) PR_LogPrint _args; \
     28    }
     29 #endif
     30 
     31 static void Error(const char* msg) { printf("\t%s\n", msg); } /* Error */
     32 
     33 static void PR_CALLBACK forked(void* arg) {
     34  PRIntn i;
     35  PRLock* ml;
     36  PRCondVar* cv;
     37 
     38  PR_LogPrint("%s logging creating mutex\n", (const char*)arg);
     39  ml = PR_NewLock();
     40  PR_LogPrint("%s logging creating condition variable\n", (const char*)arg);
     41  cv = PR_NewCondVar(ml);
     42 
     43  PR_LogPrint("%s waiting on condition timeout 10 times\n", (const char*)arg);
     44  for (i = 0; i < 10; ++i) {
     45    PR_Lock(ml);
     46    PR_WaitCondVar(cv, PR_SecondsToInterval(1));
     47    PR_Unlock(ml);
     48  }
     49 
     50  PR_LogPrint("%s logging destroying condition variable\n", (const char*)arg);
     51  PR_DestroyCondVar(cv);
     52  PR_LogPrint("%s logging destroying mutex\n", (const char*)arg);
     53  PR_DestroyLock(ml);
     54  PR_LogPrint("%s forked thread exiting\n", (const char*)arg);
     55 }
     56 
     57 static void UserLogStuff(void) {
     58  PRLogModuleInfo* myLM;
     59  PRIntn i;
     60 
     61  myLM = PR_NewLogModule("userStuff");
     62  if (!myLM) {
     63    printf("UserLogStuff(): can't create new log module\n");
     64    return;
     65  }
     66 
     67  PR_LOG(myLM, PR_LOG_NOTICE, ("Log a Notice %d\n", 1));
     68 
     69  for (i = 0; i < 10; i++) {
     70    PR_LOG(myLM, PR_LOG_DEBUG, ("Log Debug number: %d\n", i));
     71    PR_Sleep(300);
     72  }
     73 
     74 } /* end UserLogStuff() */
     75 
     76 int main(int argc, char** argv) {
     77  PRThread* thread;
     78 
     79  PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
     80 
     81  if (argc > 1) {
     82    if (!PR_SetLogFile(argv[1])) {
     83      Error("Access: Cannot create log file");
     84      goto exit;
     85    }
     86  }
     87 
     88  /* Start logging something here */
     89  PR_LogPrint("%s logging into %s\n", argv[0], argv[1]);
     90 
     91  PR_LogPrint("%s creating new thread\n", argv[0]);
     92 
     93  /*
     94  ** Now change buffering.
     95  */
     96  PR_SetLogBuffering(65500);
     97  thread = PR_CreateThread(PR_USER_THREAD, forked, (void*)argv[0],
     98                           PR_PRIORITY_NORMAL, PR_LOCAL_THREAD,
     99                           PR_JOINABLE_THREAD, 0);
    100  PR_LogPrint("%s joining thread\n", argv[0]);
    101 
    102  UserLogStuff();
    103 
    104  PR_JoinThread(thread);
    105 
    106  PR_LogFlush();
    107  return 0;
    108 
    109 exit:
    110  return -1;
    111 }
    112 
    113 /* logger.c */