tor-browser

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

timecard.c (3404B)


      1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include "timecard.h"
      8 
      9 #include <stdio.h>
     10 #include <string.h>
     11 
     12 #include "mozilla/mozalloc.h"
     13 
     14 Timecard* create_timecard() {
     15  Timecard* tc = moz_xcalloc(1, sizeof(Timecard));
     16  tc->entries_allocated = TIMECARD_INITIAL_TABLE_SIZE;
     17  tc->entries = moz_xcalloc(tc->entries_allocated, sizeof(TimecardEntry));
     18  tc->start_time = PR_Now();
     19  return tc;
     20 }
     21 
     22 void destroy_timecard(Timecard* tc) {
     23  free(tc->entries);
     24  free(tc);
     25 }
     26 
     27 void stamp_timecard(Timecard* tc, const char* event, const char* file,
     28                    unsigned int line, const char* function) {
     29  TimecardEntry* entry = NULL;
     30 
     31  /* Trim the path component from the filename */
     32  const char* last_slash = file;
     33  while (*file) {
     34    if (*file == '/' || *file == '\\') {
     35      last_slash = file;
     36    }
     37    file++;
     38  }
     39  file = last_slash;
     40  if (*file == '/' || *file == '\\') {
     41    file++;
     42  }
     43 
     44  /* Ensure there is enough space left in the entries list */
     45  if (tc->curr_entry == tc->entries_allocated) {
     46    tc->entries_allocated *= 2;
     47    tc->entries = moz_xrealloc(tc->entries,
     48                               tc->entries_allocated * sizeof(TimecardEntry));
     49  }
     50 
     51  /* Record the data into the timecard entry */
     52  entry = &tc->entries[tc->curr_entry];
     53  entry->timestamp = PR_Now();
     54  entry->event = event;
     55  entry->file = file;
     56  entry->line = line;
     57  entry->function = function;
     58  tc->curr_entry++;
     59 }
     60 
     61 void print_timecard(Timecard* tc) {
     62  size_t i;
     63  TimecardEntry* entry;
     64  size_t event_width = 5;
     65  size_t file_width = 4;
     66  size_t function_width = 8;
     67  size_t line_width;
     68  PRTime offset, delta;
     69 
     70  for (i = 0; i < tc->curr_entry; i++) {
     71    entry = &tc->entries[i];
     72    if (strlen(entry->event) > event_width) {
     73      event_width = strlen(entry->event);
     74    }
     75    if (strlen(entry->file) > file_width) {
     76      file_width = strlen(entry->file);
     77    }
     78    if (strlen(entry->function) > function_width) {
     79      function_width = strlen(entry->function);
     80    }
     81  }
     82 
     83  printf("\nTimecard created %4ld.%6.6ld\n\n",
     84         (long)(tc->start_time / PR_USEC_PER_SEC),
     85         (long)(tc->start_time % PR_USEC_PER_SEC));
     86 
     87  line_width =
     88      1 + 11 + 11 + event_width + file_width + 6 + function_width + (4 * 3);
     89 
     90  printf(" %-11s | %-11s | %-*s | %-*s | %-*s\n", "Timestamp", "Delta",
     91         (int)event_width, "Event", (int)file_width + 6, "File",
     92         (int)function_width, "Function");
     93 
     94  for (i = 0; i <= line_width; i++) {
     95    printf("=");
     96  }
     97  printf("\n");
     98 
     99  for (i = 0; i < tc->curr_entry; i++) {
    100    entry = &tc->entries[i];
    101    offset = entry->timestamp - tc->start_time;
    102    if (i > 0) {
    103      delta = entry->timestamp - tc->entries[i - 1].timestamp;
    104    } else {
    105      delta = entry->timestamp - tc->start_time;
    106    }
    107    printf(" %4ld.%6.6ld | %4ld.%6.6ld | %-*s | %*s:%-5d | %-*s\n",
    108           (long)(offset / PR_USEC_PER_SEC), (long)(offset % PR_USEC_PER_SEC),
    109           (long)(delta / PR_USEC_PER_SEC), (long)(delta % PR_USEC_PER_SEC),
    110           (int)event_width, entry->event, (int)file_width, entry->file,
    111           entry->line, (int)function_width, entry->function);
    112  }
    113  printf("\n");
    114 }