summaryrefslogtreecommitdiff
path: root/libs/JSystem/src/JAudio2/JASReport.cpp
blob: be971318685f14bed6ab3f59ee95a087d5cd39f3 (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
//
// Translation Unit: JASReport
//

#include "JSystem/JSystem.h" // IWYU pragma: keep

#include "JSystem/JAudio2/JASReport.h"
#include "JSystem/JAudio2/JASCalc.h"
#include "JSystem/JAudio2/JASMutex.h"
#include "JSystem/JKernel/JKRHeap.h"
#include <cstdio>

static OSMutex sMutex;

static char* sBuffer;

static int sLineMax;

static int sLineCount;

static int sTop;

void JASReportInit(JKRHeap* heap, int lineMax) {
    JUT_ASSERT(33, sBuffer == NULL);
    JUT_ASSERT(35, heap != NULL);
    OSInitMutex(&sMutex);
    sLineMax = lineMax;
    sBuffer = new (heap, 0) char[sLineMax * 64];
    JUT_ASSERT(41, sBuffer);
}

int JASReportGetLineMax() {
    return sLineMax;
}

int JASReportCopyBuffer(char *param_1,int lines) {
    if (!sBuffer) {
        return 0;
    }
    if (lines < 0) {
        lines = sLineMax;
    }
    char* dest = param_1;
    JASMutexLock lock(&sMutex);
    int i;
    int r30 = sTop - 1;
    char* src;
    for (i = 0; i < sLineCount && i < lines; i++) {
        if (r30 < 0) {
            r30 = sLineMax - 1;
        }
        src = &sBuffer[r30 * 64];
        JASCalc::bcopy(src, dest, 64);
        r30--;
        dest += 64;
    }
    return i;
}

void JASReport(const char * str, ...) {
    va_list vl;
    if(!sBuffer) {
        return;
    }

    va_start(vl, str);
    JASMutexLock mutexLock(&sMutex);
    vsnprintf(sBuffer + (sTop * 64), 64, str, vl);
    va_end(vl);

    sTop++;
    if (sTop >= sLineMax)
        sTop = 0;

    if (sLineCount < sLineMax)
        sLineCount++;
}