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