loaders: JPG: Fix bussy loop on corrupted file.
[gfxprim.git] / tests / framework / tst_log.c
blobb30dcb9f8deaf5a99dbce4d3292a28536f388185
1 /*****************************************************************************
2 * This file is part of gfxprim library. *
3 * *
4 * Gfxprim is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU Lesser General Public *
6 * License as published by the Free Software Foundation; either *
7 * version 2.1 of the License, or (at your option) any later version. *
8 * *
9 * Gfxprim is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
12 * Lesser General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with gfxprim; if not, write to the Free Software *
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17 * Boston, MA 02110-1301 USA *
18 * *
19 * Copyright (C) 2009-2012 Cyril Hrubis <metan@ucw.cz> *
20 * *
21 *****************************************************************************/
23 #include <sys/utsname.h>
24 #include <stdio.h>
26 #include "tst_test.h"
27 #include "tst_job.h"
28 #include "tst_msg.h"
29 #include "tst_preload.h"
30 #include "tst_timespec.h"
31 #include "tst_log.h"
33 static const char *ret_to_str(enum tst_ret ret)
35 switch (ret) {
36 case TST_SUCCESS:
37 return "Success";
38 case TST_SKIPPED:
39 return "Skipped";
40 case TST_UNTESTED:
41 return "Untested";
42 case TST_INTERR:
43 return "Internal Error";
44 case TST_SIGSEGV:
45 return "Segmentation Fault";
46 case TST_TIMEOUT:
47 return "Timeout";
48 case TST_ABORTED:
49 return "Aborted";
50 case TST_FPE:
51 return "FP Exception";
52 case TST_MEMLEAK:
53 return "Memory Leak";
54 case TST_FAILED:
55 return "Failed";
56 case TST_MAX:
57 break;
60 return "Unknown";
63 static int append_msg_json(struct tst_job *job, FILE *f)
65 struct tst_msg *msg;
67 fprintf(f, "\t\t\t\"Test Reports\": [\n");
69 for (msg = job->store.first; msg != NULL; msg = msg->next) {
70 fprintf(f, "\t\t\t\t\"%s\"", msg->msg);
72 if (msg->next != NULL)
73 fprintf(f, ",\n");
76 fprintf(f, "\n\t\t\t],\n");
78 return 0;
81 static int append_malloc_stats_json(struct tst_job *job, FILE *f)
83 fprintf(f, "\t\t\t\"Malloc Stats\": {\n");
84 fprintf(f, "\t\t\t\t\"Total Size\": %zi,\n",
85 job->malloc_stats.total_size);
86 fprintf(f, "\t\t\t\t\"Total Chunks\": %u,\n",
87 job->malloc_stats.total_chunks);
88 fprintf(f, "\t\t\t\t\"Max Size\": %zi,\n",
89 job->malloc_stats.max_size);
90 fprintf(f, "\t\t\t\t\"Max Chunks\": %u,\n",
91 job->malloc_stats.max_chunks);
92 fprintf(f, "\t\t\t\t\"Lost Size\": %zi,\n",
93 job->malloc_stats.lost_size);
94 fprintf(f, "\t\t\t\t\"Lost Chunks\": %u\n",
95 job->malloc_stats.lost_chunks);
96 fprintf(f, "\t\t\t},\n");
98 return 0;
101 static void append_benchmark_json(struct tst_job *job, FILE *f)
103 fprintf(f, "\t\t\t\"Benchmark\": {\n");
105 fprintf(f, "\t\t\t\t\"Time Mean\": %i.%09i,\n",
106 (int)job->bench_mean.tv_sec,
107 (int)job->bench_mean.tv_nsec);
109 fprintf(f, "\t\t\t\t\"Time Variance\": %i.%09i,\n",
110 (int)job->bench_var.tv_sec,
111 (int)job->bench_var.tv_nsec);
113 fprintf(f, "\t\t\t\t\"Iterations\": %i\n", job->bench_iter);
115 fprintf(f, "\t\t\t},\n");
118 static int hack_json_start = 0;
120 int tst_log_append(struct tst_job *job, FILE *f)
122 if (hack_json_start)
123 hack_json_start = 0;
124 else
125 fprintf(f, ",\n");
127 fprintf(f, "\t\t{\n");
128 fprintf(f, "\t\t\t\"Test Name\": \"%s\",\n", job->test->name);
129 fprintf(f, "\t\t\t\"Test Result\": \"%s\",\n", ret_to_str(job->result));
131 /* Append any test reports */
132 append_msg_json(job, f);
134 /* If calculated include malloc report */
135 if (job->test->flags & TST_CHECK_MALLOC)
136 append_malloc_stats_json(job, f);
138 /* If benchmark data were created */
139 if (job->bench_iter)
140 append_benchmark_json(job, f);
142 /* Time statistics */
143 int sec, nsec;
145 timespec_diff(&sec, &nsec, &job->start_time, &job->stop_time);
147 fprintf(f, "\t\t\t\"CPU Time\": %i.%09i,\n",
148 (int)job->cpu_time.tv_sec, (int)job->cpu_time.tv_nsec);
150 fprintf(f, "\t\t\t\"Run Time\": %i.%09i\n", sec, nsec);
152 fprintf(f, "\t\t}");
154 return 0;
157 static void write_system_info_json(FILE *f)
159 struct utsname buf;
161 uname(&buf);
163 fprintf(f, "\t\"System Info\": {\n");
165 fprintf(f, "\t\t\"OS\": \"%s\",\n", buf.sysname);
166 fprintf(f, "\t\t\"Hostname\": \"%s\",\n", buf.nodename);
167 fprintf(f, "\t\t\"Release\": \"%s\",\n", buf.release);
169 /* CPU related info */
170 fprintf(f, "\t\t\"CPU\": {");
172 /* lscpu is part of reasonably new util-linux */
173 FILE *cmd = popen("lscpu 2> /dev/null", "r");
175 if (cmd != NULL) {
176 char id[256], val[1024];
177 char *del = "\n";
179 while (fscanf(cmd, "%[^:]%*c %[^\n]\n", id, val) == 2) {
180 fprintf(f, "%s\t\t\t\"%s\": \"%s\"", del, id, val);
181 del = ",\n";
184 fclose(cmd);
185 fprintf(f, "\n");
188 fprintf(f, "\t\t}\n");
190 fprintf(f, "\t},\n");
193 FILE *tst_log_open(const struct tst_suite *suite, const char *path)
195 FILE *f;
197 f = fopen(path, "w");
199 if (f == NULL)
200 return NULL;
202 fprintf(f, "{\n");
203 fprintf(f, "\t\"Suite Name\": \"%s\",\n", suite->suite_name);
204 write_system_info_json(f);
205 fprintf(f, "\t\"Test Results\": [\n");
207 hack_json_start = 1;
209 return f;
212 int tst_log_close(FILE *f)
214 fprintf(f, "\n\t]\n}\n");
215 return fclose(f);