xref: /aosp_15_r20/external/OpenCL-ICD-Loader/test/log/icd_test_log.c (revision 1cddb830dba8aa7c1cc1039338e56b3b9fa24952)
1 #include<sys/stat.h>
2 #include<stdlib.h>
3 #include<stdio.h>
4 #include<stdarg.h>
5 #include<CL/cl.h>
6 #include<platform/icd_test_log.h>
7 
8 #define APP_LOG_FILE  "icd_test_app_log.txt"
9 #define STUB_LOG_FILE "icd_test_stub_log.txt"
10 
11 static FILE *app_log_file;
12 static FILE *stub_log_file;
13 
test_icd_initialize_app_log(void)14 int test_icd_initialize_app_log(void)
15 {
16     app_log_file = fopen(APP_LOG_FILE, "w");
17     if (!app_log_file) {
18 		printf("Unable to open file %s\n", APP_LOG_FILE);
19         return -1;
20     }
21 
22     return 0;
23 }
24 
test_icd_close_app_log(void)25 void test_icd_close_app_log(void)
26 {
27     fclose(app_log_file);
28 }
29 
test_icd_app_log(const char * format,...)30 void test_icd_app_log(const char *format, ...)
31 {
32     va_list args;
33     va_start(args, format);
34     vfprintf(app_log_file, format, args);
35     va_end(args);
36 }
37 
test_icd_initialize_stub_log(void)38 int test_icd_initialize_stub_log(void)
39 {
40    	stub_log_file = fopen(STUB_LOG_FILE, "w");
41     if (!stub_log_file) {
42 		printf("Unable to open file %s\n", STUB_LOG_FILE);
43         return -1;
44     }
45 
46     return 0;
47 
48 }
49 
test_icd_close_stub_log(void)50 void test_icd_close_stub_log(void)
51 {
52     fclose(stub_log_file);
53 }
54 
test_icd_stub_log(const char * format,...)55 void test_icd_stub_log(const char *format, ...)
56 {
57     va_list args;
58     va_start(args, format);
59     vfprintf(stub_log_file, format, args);
60     va_end(args);
61 }
62 
test_icd_get_log(const char * filename)63 static char *test_icd_get_log(const char *filename)
64 {
65     struct stat statbuf;
66     FILE *fp;
67     char *source = NULL;
68 
69     fp = fopen(filename, "rb");
70 
71     if (fp) {
72         size_t fsize = 0;
73         stat(filename, &statbuf);
74         fsize = statbuf.st_size;
75         source = (char *)malloc(fsize+1); // +1 for NULL terminator
76         if (source) {
77             if (fsize) {
78                 if (fread(source, fsize, 1, fp) != 1) {
79                     free(source);
80                     source = NULL;
81                 } else {
82                     source[fsize] = '\0';
83                 }
84             } else {
85                 // Don't fail when fsize = 0, just return empty string
86                 source[fsize] = '\0';
87             }
88         }
89         fclose(fp);
90     }
91 
92     return source;
93 }
94 
test_icd_get_app_log(void)95 char *test_icd_get_app_log(void)
96 {
97     return test_icd_get_log(APP_LOG_FILE);
98 }
99 
test_icd_get_stub_log(void)100 char *test_icd_get_stub_log(void)
101 {
102     return test_icd_get_log(STUB_LOG_FILE);
103 }
104