Line data Source code
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 <stdio.h>
8 : #include "timecard.h"
9 : #include "mozilla/mozalloc.h"
10 :
11 : Timecard *
12 0 : create_timecard()
13 : {
14 0 : Timecard *tc = moz_xcalloc(1,sizeof(Timecard));
15 0 : tc->entries_allocated = TIMECARD_INITIAL_TABLE_SIZE;
16 0 : tc->entries = moz_xcalloc(tc->entries_allocated, sizeof(TimecardEntry));
17 0 : tc->start_time = PR_Now();
18 0 : return tc;
19 : }
20 :
21 : void
22 0 : destroy_timecard(Timecard *tc)
23 : {
24 0 : free(tc->entries);
25 0 : free(tc);
26 0 : }
27 :
28 : void
29 0 : stamp_timecard(Timecard *tc,
30 : const char *event,
31 : const char *file,
32 : unsigned int line,
33 : const char *function)
34 : {
35 0 : TimecardEntry *entry = NULL;
36 :
37 : /* Trim the path component from the filename */
38 0 : const char *last_slash = file;
39 0 : while (*file) {
40 0 : if (*file == '/' || *file == '\\') {
41 0 : last_slash = file;
42 : }
43 0 : file++;
44 : }
45 0 : file = last_slash;
46 0 : if (*file == '/' || *file == '\\') {
47 0 : file++;
48 : }
49 :
50 : /* Ensure there is enough space left in the entries list */
51 0 : if (tc->curr_entry == tc->entries_allocated) {
52 0 : tc->entries_allocated *= 2;
53 0 : tc->entries = moz_xrealloc(tc->entries,
54 0 : tc->entries_allocated * sizeof(TimecardEntry));
55 : }
56 :
57 : /* Record the data into the timecard entry */
58 0 : entry = &tc->entries[tc->curr_entry];
59 0 : entry->timestamp = PR_Now();
60 0 : entry->event = event;
61 0 : entry->file = file;
62 0 : entry->line = line;
63 0 : entry->function = function;
64 0 : tc->curr_entry++;
65 0 : }
66 :
67 : void
68 0 : print_timecard(Timecard *tc)
69 : {
70 : size_t i;
71 : TimecardEntry *entry;
72 0 : size_t event_width = 5;
73 0 : size_t file_width = 4;
74 0 : size_t function_width = 8;
75 : size_t line_width;
76 : PRTime offset, delta;
77 :
78 0 : for (i = 0; i < tc->curr_entry; i++) {
79 0 : entry = &tc->entries[i];
80 0 : if (strlen(entry->event) > event_width) {
81 0 : event_width = strlen(entry->event);
82 : }
83 0 : if (strlen(entry->file) > file_width) {
84 0 : file_width = strlen(entry->file);
85 : }
86 0 : if (strlen(entry->function) > function_width) {
87 0 : function_width = strlen(entry->function);
88 : }
89 : }
90 :
91 0 : printf("\nTimecard created %4ld.%6.6ld\n\n",
92 0 : (long)(tc->start_time / PR_USEC_PER_SEC),
93 0 : (long)(tc->start_time % PR_USEC_PER_SEC));
94 :
95 0 : line_width = 1 + 11 + 11 + event_width + file_width + 6 +
96 : function_width + (4 * 3);
97 :
98 0 : printf(" %-11s | %-11s | %-*s | %-*s | %-*s\n",
99 : "Timestamp", "Delta",
100 : (int)event_width, "Event",
101 0 : (int)file_width + 6, "File",
102 : (int)function_width, "Function");
103 :
104 0 : for (i = 0; i <= line_width; i++) {
105 0 : printf("=");
106 : }
107 0 : printf("\n");
108 :
109 0 : for (i = 0; i < tc->curr_entry; i++) {
110 0 : entry = &tc->entries[i];
111 0 : offset = entry->timestamp - tc->start_time;
112 0 : if (i > 0) {
113 0 : delta = entry->timestamp - tc->entries[i-1].timestamp;
114 : } else {
115 0 : delta = entry->timestamp - tc->start_time;
116 : }
117 0 : printf(" %4ld.%6.6ld | %4ld.%6.6ld | %-*s | %*s:%-5d | %-*s\n",
118 : (long)(offset / PR_USEC_PER_SEC), (long)(offset % PR_USEC_PER_SEC),
119 : (long)(delta / PR_USEC_PER_SEC), (long)(delta % PR_USEC_PER_SEC),
120 : (int)event_width, entry->event,
121 : (int)file_width, entry->file, entry->line,
122 : (int)function_width, entry->function);
123 : }
124 0 : printf("\n");
125 0 : }
|