aboutsummaryrefslogtreecommitdiffstats
path: root/longbow/src/LongBow/Reporting/ANSITerm/longBowReport_Runtime.c
blob: 60ca4d42b4672fa92acf92ded14c50a735f6bfa1 (plain)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 */
#include <config.h>

#include <stdio.h>
#include <stdarg.h>
#include <assert.h>

#include <LongBow/Reporting/ANSITerm/longBowReport_Runtime.h>
#include <LongBow/private/longBow_Memory.h>

static const char *ansiRed = "\x1b[31m";
static const char *ansiGreen = "\x1b[32m";
static const char *ansiYellow = "\x1b[33m";
static const char *ansiMagenta = "\x1b[35m";
static const char *ansiReset = "\x1b[0m";

static void
_printGreen(void)
{
    printf("%s", ansiGreen);
}

static void
_printYellow(void)
{
    printf("%s", ansiYellow);
}

static void
_printMagenta(void)
{
    printf("%s", ansiMagenta);
}

static void
_printReset(void)
{
    printf("%s", ansiReset);
    fflush(stdout);
}

static void
_longBowReportRuntime_RedPrintf(const char *format, va_list args)
{
    longBowReportRuntime_PrintRed();
    vprintf(format, args);
    _printReset();
}

static void
_longBowReportRuntime_YellowPrintf(const char *format, va_list args)
{
    _printYellow();
    vprintf(format, args);
    _printReset();
}

void
longBowReportRuntime_PrintRed(void)
{
    printf("%s", ansiRed);
}

void
longBowReportRuntime_RedPrintf(const char *format, ...)
{
    va_list args;
    va_start(args, format);
    _longBowReportRuntime_RedPrintf(format, args);
    va_end(args);
}

void
longBowReportRuntime_GreenPrintf(const char *format, ...)
{
    _printGreen();
    va_list args;
    va_start(args, format);
    vprintf(format, args);
    va_end(args);
    _printReset();
}

void
longBowReportRuntime_MagentaPrintf(const char *format, ...)
{
    _printMagenta();
    va_list args;
    va_start(args, format);
    vprintf(format, args);
    va_end(args);
    _printReset();
}

void
longBowReportRuntime_YellowPrintf(const char *format, ...)
{
    va_list args;
    va_start(args, format);

    _longBowReportRuntime_YellowPrintf(format, args);

    va_end(args);
}

void
longBowReportRuntime_ParseSuppress(LongBowReportConfig *result, const char *key)
{
    for (size_t i = 0; i < strlen(key); i++) {
        if (*key == 'X') {
            result->suppress_report.untested = 1;
        } else if (*key == '.') {
            result->suppress_report.succeeded = 1;
        } else if (*key == 'S') {
            result->suppress_report.skipped = 1;
        } else if (*key == 'W') {
            result->suppress_report.warned = 1;
        } else if (*key == 's') {
            result->suppress_report.setup_failed = 1;
        } else if (*key == 't') {
            result->suppress_report.teardown_failed = 1;
        } else if (*key == 'w') {
            result->suppress_report.teardown_warned = 1;
        } else if (*key == 'F') {
            result->suppress_report.failed = 1;
        } else if (*key == 'T') {
            result->suppress_report.stopped = 1;
        } else if (*key == 'U') {
            result->suppress_report.unimplemented = 1;
        } else {
            printf("Unknown suppression key '%c'\n", *key);
        }
    }
}

LongBowReportConfig *
longBowReportRuntime_Create(int argc, char *argv[])
{
    static const char *prefix = "--report";
    size_t prefixLength = strlen(prefix);

    LongBowReportConfig *result = longBowMemory_Allocate(sizeof(LongBowReportConfig));

    for (int i = 0; i < argc; i++) {
        if (strncmp(prefix, argv[i], prefixLength) == 0) {
            if (strcmp("--report-suppress", argv[i]) == 0) {
                longBowReportRuntime_ParseSuppress(result, argv[i + 1]);
                i++;
            }
        } else if (strcmp("--help", argv[i]) == 0) {
            printf("Options for LongBow Report ANSI Terminal\n");
            printf("  --report-suppress [STFU.XWstw] Suppress the display of specific reports.\n");
            printf("     S - suppress the report of a skipped test.\n");
            printf("     T - suppress the report of a stopped test.\n");
            printf("     F - suppress the report of a failed test.\n");
            printf("     U - suppress the report of an unimplemented test.\n");
            printf("     . - suppress the report of a successful test.\n");
            printf("     X - suppress the report of an untested test.\n");
            printf("     W - suppress the report of a warned test.\n");
            printf("     s - suppress the report of a setup failure.\n");
            printf("     t - suppress the report of a tear-down failure.\n");
            printf("     w - suppress the report of a tear-down warning.\n");
            free(result);
            return NULL;
        }
    }

    return result;
}

void
longBowReportRuntime_Destroy(LongBowReportConfig **reportPtr)
{
    longBowMemory_Deallocate((void **) reportPtr);
}

static void
_EventPrint(const LongBowEvent *event)
{
    if (longBowEventType_IsSuppressAlert(longBowEvent_GetEventType(event)) == false) {
        char *location = longBowLocation_ToString(longBowEvent_GetLocation(event));
        printf("%s %s %s %s\r\n",
               longBowEvent_GetName(event), location, longBowEvent_GetKind(event), longBowEvent_GetMessage(event));

        if (longBowEventType_IsSuppressBacktrace(longBowEvent_GetEventType(event)) == false) {
            char **strs = longBowEvent_CreateSymbolicCallstack(event);
            if (strs != NULL) {
                for (size_t i = 0; i < longBowEvent_GetCallStackLength(event); ++i) {
                    printf("%s\r\n", strs[i]);
                }
                free(strs);
            }
        }
        fflush(stdout);
        free(location);
    }
}

void
longBowReportRuntime_Event(const LongBowEvent *event)
{
    LongBowStatus status = longBowEventType_GetStatus(longBowEvent_GetEventType(event));
    switch (status) {
        case LongBowStatus_DONTCARE:
        case LongBowStatus_UNTESTED:
            break;

        /* successful */
        case LONGBOW_STATUS_SUCCEEDED:
            // If this happens, there is an error in the encoding of the LongBowEventType.
            longBowReportRuntime_PrintRed();
            _EventPrint(event);
            _printReset();
            break;

        case LongBowStatus_WARNED:
        case LongBowStatus_TEARDOWN_WARNED:
        case LONGBOW_STATUS_SKIPPED:
        case LongBowStatus_UNIMPLEMENTED:
        case LongBowStatus_IMPOTENT:
        case LONGBOW_STATUS_MEMORYLEAK:
        case LONGBOW_STATUS_SETUP_SKIPTESTS:
            _printYellow();
            _EventPrint(event);
            _printReset();
            break;

        /* failure */
        case LONGBOW_STATUS_FAILED:
        case LongBowStatus_STOPPED:
        case LONGBOW_STATUS_TEARDOWN_FAILED:
        case LONGBOW_STATUS_SETUP_FAILED:
        case LongBowStatus_SIGNALLED:
            longBowReportRuntime_PrintRed();
            _EventPrint(event);
            _printReset();
            break;

        case LongBowStatus_LIMIT: // fall through
        default:
            _printYellow();
            _EventPrint(event);
            _printReset();
            break;
    }
}

void
longBowReportRuntime_Message(const char *format, ...)
{
    va_list args;
    va_start(args, format);
    vprintf(format, args);
    va_end(args);
}

void
longBowReportRuntime_Warning(const char *format, ...)
{
    va_list args;
    va_start(args, format);
    longBowReportRuntime_YellowPrintf("WARNING ");
    _longBowReportRuntime_YellowPrintf(format, args);
    va_end(args);
}

void
longBowReportRuntime_Error(const char *format, ...)
{
    va_list args;
    va_start(args, format);
    longBowReportRuntime_RedPrintf("FAILURE ");
    _longBowReportRuntime_RedPrintf(format, args);
    va_end(args);
}