michael@0: /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=8 sts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include michael@0: #include "timecard.h" michael@0: #include "mozilla/mozalloc.h" michael@0: michael@0: Timecard * michael@0: create_timecard() michael@0: { michael@0: Timecard *tc = moz_xcalloc(1,sizeof(Timecard)); michael@0: tc->entries_allocated = TIMECARD_INITIAL_TABLE_SIZE; michael@0: tc->entries = moz_xcalloc(tc->entries_allocated, sizeof(TimecardEntry)); michael@0: tc->start_time = PR_Now(); michael@0: return tc; michael@0: } michael@0: michael@0: void michael@0: destroy_timecard(Timecard *tc) michael@0: { michael@0: moz_free(tc->entries); michael@0: moz_free(tc); michael@0: } michael@0: michael@0: void michael@0: stamp_timecard(Timecard *tc, michael@0: const char *event, michael@0: const char *file, michael@0: unsigned int line, michael@0: const char *function) michael@0: { michael@0: TimecardEntry *entry = NULL; michael@0: michael@0: /* Trim the path component from the filename */ michael@0: const char *last_slash = file; michael@0: while (*file) { michael@0: if (*file == '/' || *file == '\\') { michael@0: last_slash = file; michael@0: } michael@0: file++; michael@0: } michael@0: file = last_slash; michael@0: if (*file == '/' || *file == '\\') { michael@0: file++; michael@0: } michael@0: michael@0: /* Ensure there is enough space left in the entries list */ michael@0: if (tc->curr_entry == tc->entries_allocated) { michael@0: tc->entries_allocated *= 2; michael@0: tc->entries = moz_xrealloc(tc->entries, michael@0: tc->entries_allocated * sizeof(TimecardEntry)); michael@0: } michael@0: michael@0: /* Record the data into the timecard entry */ michael@0: entry = &tc->entries[tc->curr_entry]; michael@0: entry->timestamp = PR_Now(); michael@0: entry->event = event; michael@0: entry->file = file; michael@0: entry->line = line; michael@0: entry->function = function; michael@0: tc->curr_entry++; michael@0: } michael@0: michael@0: void michael@0: print_timecard(Timecard *tc) michael@0: { michael@0: size_t i; michael@0: TimecardEntry *entry; michael@0: size_t event_width = 5; michael@0: size_t file_width = 4; michael@0: size_t function_width = 8; michael@0: size_t line_width; michael@0: PRTime offset, delta; michael@0: michael@0: for (i = 0; i < tc->curr_entry; i++) { michael@0: entry = &tc->entries[i]; michael@0: if (strlen(entry->event) > event_width) { michael@0: event_width = strlen(entry->event); michael@0: } michael@0: if (strlen(entry->file) > file_width) { michael@0: file_width = strlen(entry->file); michael@0: } michael@0: if (strlen(entry->function) > function_width) { michael@0: function_width = strlen(entry->function); michael@0: } michael@0: } michael@0: michael@0: printf("\nTimecard created %4lld.%6.6lld\n\n", michael@0: tc->start_time / PR_USEC_PER_SEC, tc->start_time % PR_USEC_PER_SEC); michael@0: michael@0: line_width = 1 + 11 + 11 + event_width + file_width + 6 + michael@0: function_width + (4 * 3); michael@0: michael@0: printf(" %-11s | %-11s | %-*s | %-*s | %-*s\n", michael@0: "Timestamp", "Delta", michael@0: (int)event_width, "Event", michael@0: (int)file_width + 6, "File", michael@0: (int)function_width, "Function"); michael@0: michael@0: for (i = 0; i <= line_width; i++) { michael@0: printf("="); michael@0: } michael@0: printf("\n"); michael@0: michael@0: for (i = 0; i < tc->curr_entry; i++) { michael@0: entry = &tc->entries[i]; michael@0: offset = entry->timestamp - tc->start_time; michael@0: if (i > 0) { michael@0: delta = entry->timestamp - tc->entries[i-1].timestamp; michael@0: } else { michael@0: delta = entry->timestamp - tc->start_time; michael@0: } michael@0: printf(" %4lld.%6.6lld | %4lld.%6.6lld | %-*s | %*s:%-5d | %-*s\n", michael@0: offset / PR_USEC_PER_SEC, offset % PR_USEC_PER_SEC, michael@0: delta / PR_USEC_PER_SEC, delta % PR_USEC_PER_SEC, michael@0: (int)event_width, entry->event, michael@0: (int)file_width, entry->file, entry->line, michael@0: (int)function_width, entry->function); michael@0: } michael@0: printf("\n"); michael@0: }