1 /*
2  * Copyright (c) 2017 Oticon A/S
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #include <stdlib.h>
7 #include <string.h>
8 #include <strings.h>
9 #include "bs_oswrap.h"
10 #include "bs_results.h"
11 #include "bs_tracing.h"
12 #include "bs_dump_files.h"
13 
14 #define BS_ALLOC_CHUNK 10
15 static bs_dumpf_ctrl_t *df_ctrl; /* Array of dump file control elements */
16 static int bsdc_alloc_size; /* Number of allocated control elements */
17 static int number_of_dump_files; /* Number of used control elements/dump files */
18 
bs_dump_file_register(bs_dumpf_ctrl_t * f_ctrl)19 int bs_dump_file_register(bs_dumpf_ctrl_t *f_ctrl) {
20   if (number_of_dump_files + 1 >= bsdc_alloc_size) {
21     bsdc_alloc_size += BS_ALLOC_CHUNK;
22     df_ctrl = bs_realloc(df_ctrl, bsdc_alloc_size * sizeof(bs_dumpf_ctrl_t) );
23   }
24   memcpy(&df_ctrl[number_of_dump_files], f_ctrl, sizeof(bs_dumpf_ctrl_t));
25   number_of_dump_files++;
26 
27   return number_of_dump_files - 1;
28 }
29 
bs_dump_files_set_dump_level(int newdumplevel)30 void bs_dump_files_set_dump_level(int newdumplevel) {
31   for (unsigned int i = 0 ; i < number_of_dump_files; i ++) {
32     if (df_ctrl[i].dump_level <= newdumplevel) {
33       df_ctrl[i].enabled = true;
34     }
35   }
36 }
37 
bs_dump_files_activate_file(const char * filename)38 void bs_dump_files_activate_file(const char *filename) {
39   bool all = false;
40   bool none = false;
41 
42   if (strcasecmp(filename,"all") == 0) {
43     all = true;
44   } else if (strcasecmp(filename,"none") == 0) {
45     none = true;
46   }
47 
48   for (unsigned int i = 0; i < number_of_dump_files; i ++) {
49     if (none) {
50       df_ctrl[i].enabled = false;
51       bs_trace_raw(9, "dump: Dump of file %s de-activated\n",
52           df_ctrl[i].postfix);
53     } else if (all || (strcmp(filename,df_ctrl[i].postfix) == 0)) {
54       df_ctrl[i].enabled = true;
55       bs_trace_raw(9,"dump: Dump of file %s activated\n",
56           df_ctrl[i].postfix);
57     }
58   }
59 }
60 
61 /**
62  * Print to console all files names, their default dump level
63  * and if they are currently active or not
64  */
bs_dump_files_print_files(void)65 void bs_dump_files_print_files(void) {
66   bs_trace_raw(0," File name (suffix)            , dump_level,"
67       " activated\n");
68   for (unsigned int i = 0; i < number_of_dump_files; i ++) {
69     bs_trace_raw(0, " %-30s,     %2i    ,     %i\n",
70                  df_ctrl[i].postfix,
71                  df_ctrl[i].dump_level,
72                  df_ctrl[i].enabled);
73   }
74 }
75 
bs_dump_files_open(const char * s,const unsigned int dev_number)76 void bs_dump_files_open(const char* s, const unsigned int dev_number) {
77   char* results_path = NULL;
78   bool results_folder_created = false;
79 
80   for (unsigned int i = 0 ; i < number_of_dump_files; i ++) {
81     if (df_ctrl[i].enabled == false) {
82       continue;
83     }
84     if (results_folder_created == false) {
85       results_path = bs_create_result_folder(s);
86       results_folder_created = true;
87     }
88 
89     char filename[strlen(df_ctrl[i].postfix) + strlen(results_path) + 25];
90 
91     sprintf(filename, "%s/d_%02i.%s.csv", results_path,
92         dev_number, df_ctrl[i].postfix);
93 
94     df_ctrl[i].fileptr = bs_fopen(filename, "wt");
95 
96     if (df_ctrl[i].header_f != NULL) {
97       df_ctrl[i].header_f(df_ctrl[i].fileptr);
98     }
99   }
100 
101   if (results_path != NULL) {
102     free(results_path);
103   }
104 }
105 
bs_dump_files_close_all(void)106 void bs_dump_files_close_all(void) {
107   if (!df_ctrl) {
108     return;
109   }
110 
111   for (unsigned int i = 0 ; i < number_of_dump_files; i ++) {
112     if (df_ctrl[i].fileptr != NULL) {
113       fclose(df_ctrl[i].fileptr);
114       df_ctrl[i].fileptr = NULL;
115     }
116   }
117 
118   free(df_ctrl);
119   df_ctrl = NULL;
120 }
121 
122 
bsdf_cmd_dump_found(char * argv,int offset)123 void bsdf_cmd_dump_found(char * argv, int offset) {
124 	bs_dump_files_activate_file(&argv[offset]);
125 }
126 
bsdf_cmd_printdumps_found(char * argv,int offset)127 void bsdf_cmd_printdumps_found(char * argv, int offset) {
128 	bs_dump_files_print_files();
129 }
130 
131 unsigned int bsdf_dump_level;
bsdf_cmd_dumplevel_found(char * argv,int offset)132 void bsdf_cmd_dumplevel_found(char * argv, int offset) {
133 	bs_dump_files_set_dump_level(bsdf_dump_level);
134 }
135