-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample4_logging.c
More file actions
69 lines (54 loc) · 1.46 KB
/
example4_logging.c
File metadata and controls
69 lines (54 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
* Example 4: File Logging
*
* Demonstrates automatic logging to files
*/
#include "profiler.h"
#include <unistd.h>
void simulate_work(int ms) {
usleep(ms * 1000);
}
void logged_function(void) {
PROFILER_FUNCTION();
simulate_work(15);
}
int main(void) {
profiler_init();
printf("=== Example 4: File Logging ===\n\n");
/* Enable file logging */
profiler_set_log_file("profiler.log");
printf("Logging to profiler.log...\n");
/* These will be automatically logged */
{
PROFILER_SCOPE(operation_1);
simulate_work(10);
}
{
PROFILER_SCOPE(operation_2);
simulate_work(20);
}
logged_function();
/* Manual timer with logging */
profiler_timer_t timer;
profiler_timer_start(&timer, "manual_logged");
simulate_work(30);
profiler_timer_stop(&timer);
/* Also log statistics to file */
profiler_stats_t stats;
profiler_stats_init(&stats, "batch_operations");
for (int i = 0; i < 5; i++) {
profiler_timer_t t;
profiler_timer_start(&t, "batch_op");
simulate_work(8 + i * 2);
profiler_timer_stop(&t);
profiler_stats_add(&stats, profiler_timer_elapsed_ns(&t));
}
FILE *log = fopen("profiler.log", "a");
if (log) {
profiler_stats_log(&stats, log);
fclose(log);
}
printf("Results logged to profiler.log\n");
profiler_shutdown();
return 0;
}