Skip to content

Commit 14b83d3

Browse files
committed
use tprintf instead of printf to be able disable messages by quiet option
(issue #1240)
1 parent d937266 commit 14b83d3

File tree

2 files changed

+45
-45
lines changed

2 files changed

+45
-45
lines changed

src/opencl/openclwrapper.cpp

+40-40
Original file line numberDiff line numberDiff line change
@@ -553,30 +553,30 @@ static ds_status writeProfileToFile(ds_profile* profile,
553553

554554
// substitute invalid characters in device name with _
555555
static void legalizeFileName(char* fileName) {
556-
// printf("fileName: %s\n", fileName);
556+
// tprintf("fileName: %s\n", fileName);
557557
const char* invalidChars =
558558
"/\?:*\"><| "; // space is valid but can cause headaches
559559
// for each invalid char
560560
for (unsigned i = 0; i < strlen(invalidChars); i++) {
561561
char invalidStr[4];
562562
invalidStr[0] = invalidChars[i];
563563
invalidStr[1] = '\0';
564-
// printf("eliminating %s\n", invalidStr);
564+
// tprintf("eliminating %s\n", invalidStr);
565565
// char *pos = strstr(fileName, invalidStr);
566566
// initial ./ is valid for present directory
567567
// if (*pos == '.') pos++;
568568
// if (*pos == '/') pos++;
569569
for (char* pos = strstr(fileName, invalidStr); pos != nullptr;
570570
pos = strstr(pos + 1, invalidStr)) {
571-
// printf("\tfound: %s, ", pos);
571+
// tprintf("\tfound: %s, ", pos);
572572
pos[0] = '_';
573-
// printf("fileName: %s\n", fileName);
573+
// tprintf("fileName: %s\n", fileName);
574574
}
575575
}
576576
}
577577

578578
static void populateGPUEnvFromDevice(GPUEnv* gpuInfo, cl_device_id device) {
579-
// printf("[DS] populateGPUEnvFromDevice\n");
579+
// tprintf("[DS] populateGPUEnvFromDevice\n");
580580
size_t size;
581581
gpuInfo->mnIsUserCreated = 1;
582582
// device
@@ -713,7 +713,7 @@ int OpenclDevice::initMorphCLAllocations(l_int32 wpl, l_int32 h, Pix* pixs) {
713713

714714
int OpenclDevice::InitEnv() {
715715
// PERF_COUNT_START("OD::InitEnv")
716-
// printf("[OD] OpenclDevice::InitEnv()\n");
716+
// tprintf("[OD] OpenclDevice::InitEnv()\n");
717717
#ifdef SAL_WIN32
718718
while (1) {
719719
if (1 == LoadOpencl()) break;
@@ -763,7 +763,7 @@ int OpenclDevice::InitOpenclRunEnv_DeviceSelection(int argc) {
763763
cl_device_id bestDevice = bestDevice_DS.oclDeviceID;
764764
// overwrite global static GPUEnv with new device
765765
if (selectedDeviceIsOpenCL()) {
766-
// printf("[DS] InitOpenclRunEnv_DS::Calling populateGPUEnvFromDevice()
766+
// tprintf("[DS] InitOpenclRunEnv_DS::Calling populateGPUEnvFromDevice()
767767
// for selected device\n");
768768
populateGPUEnvFromDevice(&gpuEnv, bestDevice);
769769
gpuEnv.mnFileCount = 0; // argc;
@@ -772,7 +772,7 @@ int OpenclDevice::InitOpenclRunEnv_DeviceSelection(int argc) {
772772
CompileKernelFile(&gpuEnv, "");
773773
// PERF_COUNT_SUB("CompileKernelFile")
774774
} else {
775-
// printf("[DS] InitOpenclRunEnv_DS::Skipping populateGPUEnvFromDevice()
775+
// tprintf("[DS] InitOpenclRunEnv_DS::Skipping populateGPUEnvFromDevice()
776776
// b/c native cpu selected\n");
777777
}
778778
isInited = 1;
@@ -925,10 +925,10 @@ int OpenclDevice::GeneratBinFromKernelSource(cl_program program,
925925
sprintf(fileName, "%s-%s.bin", cl_name, deviceName);
926926
legalizeFileName(fileName);
927927
if (!WriteBinaryToFile(fileName, binaries[i], binarySizes[i])) {
928-
printf("[OD] write binary[%s] failed\n", fileName);
928+
tprintf("[OD] write binary[%s] failed\n", fileName);
929929
return 0;
930930
} // else
931-
printf("[OD] write binary[%s] successfully\n", fileName);
931+
tprintf("[OD] write binary[%s] successfully\n", fileName);
932932
}
933933
}
934934

@@ -1017,7 +1017,7 @@ int OpenclDevice::CompileKernelFile(GPUEnv* gpuInfo, const char* buildOption) {
10171017

10181018
// char options[512];
10191019
// create a cl program executable for all the devices specified
1020-
// printf("[OD] BuildProgram.\n");
1020+
// tprintf("[OD] BuildProgram.\n");
10211021
PERF_COUNT_START("OD::CompileKernel::clBuildProgram")
10221022
if (!gpuInfo->mnIsUserCreated) {
10231023
clStatus =
@@ -1032,7 +1032,7 @@ int OpenclDevice::CompileKernelFile(GPUEnv* gpuInfo, const char* buildOption) {
10321032
}
10331033
PERF_COUNT_END
10341034
if (clStatus != CL_SUCCESS) {
1035-
printf("BuildProgram error!\n");
1035+
tprintf("BuildProgram error!\n");
10361036
size_t length;
10371037
if (!gpuInfo->mnIsUserCreated) {
10381038
clStatus = clGetProgramBuildInfo(
@@ -1044,7 +1044,7 @@ int OpenclDevice::CompileKernelFile(GPUEnv* gpuInfo, const char* buildOption) {
10441044
CL_PROGRAM_BUILD_LOG, 0, nullptr, &length);
10451045
}
10461046
if (clStatus != CL_SUCCESS) {
1047-
printf("opencl create build log fail\n");
1047+
tprintf("opencl create build log fail\n");
10481048
return 0;
10491049
}
10501050
std::vector<char> buildLog(length);
@@ -1058,7 +1058,7 @@ int OpenclDevice::CompileKernelFile(GPUEnv* gpuInfo, const char* buildOption) {
10581058
length, &buildLog[0], &length);
10591059
}
10601060
if (clStatus != CL_SUCCESS) {
1061-
printf("opencl program build info fail\n");
1061+
tprintf("opencl program build info fail\n");
10621062
return 0;
10631063
}
10641064

@@ -1890,7 +1890,7 @@ int OpenclDevice::ThresholdRectToPixOCL(unsigned char* imageData,
18901890
clFinish(rEnv.mpkCmdQueue);
18911891
PERF_COUNT_SUB("kernel")
18921892
if (clStatus != 0) {
1893-
printf("Setting return value to -1\n");
1893+
tprintf("Setting return value to -1\n");
18941894
retVal = -1;
18951895
}
18961896
/* map results back from gpu */
@@ -1954,7 +1954,7 @@ static void populateTessScoreEvaluationInputData(
19541954
for (int i = 0; i < numLines; i++) {
19551955
int lineWidth = rand() % maxLineWidth;
19561956
int vertLinePos = lineWidth + rand() % (width - 2 * lineWidth);
1957-
// printf("[PI] VerticalLine @ %i (w=%i)\n", vertLinePos, lineWidth);
1957+
// tprintf("[PI] VerticalLine @ %i (w=%i)\n", vertLinePos, lineWidth);
19581958
for (int row = vertLinePos - lineWidth / 2;
19591959
row < vertLinePos + lineWidth / 2; row++) {
19601960
for (int col = 0; col < height; col++) {
@@ -1970,13 +1970,13 @@ static void populateTessScoreEvaluationInputData(
19701970
for (int i = 0; i < numLines; i++) {
19711971
int lineWidth = rand() % maxLineWidth;
19721972
int horLinePos = lineWidth + rand() % (height - 2 * lineWidth);
1973-
// printf("[PI] HorizontalLine @ %i (w=%i)\n", horLinePos, lineWidth);
1973+
// tprintf("[PI] HorizontalLine @ %i (w=%i)\n", horLinePos, lineWidth);
19741974
for (int row = 0; row < width; row++) {
19751975
for (int col = horLinePos - lineWidth / 2;
19761976
col < horLinePos + lineWidth / 2;
19771977
col++) { // for (int row = vertLinePos-lineWidth/2; row <
19781978
// vertLinePos+lineWidth/2; row++) {
1979-
// printf("[PI] HoizLine pix @ (%3i, %3i)\n", row, col);
1979+
// tprintf("[PI] HoizLine pix @ (%3i, %3i)\n", row, col);
19801980
// imageData4[row*width+col] = pixelBlack;
19811981
imageData4[row * width + col][0] = pixelBlack[0];
19821982
imageData4[row * width + col][1] = pixelBlack[1];
@@ -1993,10 +1993,10 @@ static void populateTessScoreEvaluationInputData(
19931993
int lineWidth = rand() % maxLineWidth;
19941994
int col = lineWidth + rand() % (width - 2 * lineWidth);
19951995
int row = lineWidth + rand() % (height - 2 * lineWidth);
1996-
// printf("[PI] Spot[%i/%i] @ (%3i, %3i)\n", i, numSpots, row, col );
1996+
// tprintf("[PI] Spot[%i/%i] @ (%3i, %3i)\n", i, numSpots, row, col );
19971997
for (int r = row - lineWidth / 2; r < row + lineWidth / 2; r++) {
19981998
for (int c = col - lineWidth / 2; c < col + lineWidth / 2; c++) {
1999-
// printf("[PI] \tSpot[%i/%i] @ (%3i, %3i)\n", i, numSpots, r, c );
1999+
// tprintf("[PI] \tSpot[%i/%i] @ (%3i, %3i)\n", i, numSpots, r, c );
20002000
// imageData4[row*width+col] = pixelBlack;
20012001
imageData4[r * width + c][0] = pixelBlack[0];
20022002
imageData4[r * width + c][1] = pixelBlack[1];
@@ -2444,17 +2444,17 @@ static ds_status releaseScore(TessDeviceScore* score) {
24442444
static ds_status evaluateScoreForDevice(ds_device* device, void* inputData) {
24452445
// overwrite statuc gpuEnv w/ current device
24462446
// so native opencl calls can be used; they use static gpuEnv
2447-
printf("\n[DS] Device: \"%s\" (%s) evaluation...\n", device->oclDeviceName,
2447+
tprintf("\n[DS] Device: \"%s\" (%s) evaluation...\n", device->oclDeviceName,
24482448
device->type == DS_DEVICE_OPENCL_DEVICE ? "OpenCL" : "Native");
24492449
GPUEnv* env = nullptr;
24502450
if (device->type == DS_DEVICE_OPENCL_DEVICE) {
24512451
env = &OpenclDevice::gpuEnv;
24522452
memset(env, 0, sizeof(*env));
2453-
// printf("[DS] populating tmp GPUEnv from device\n");
2453+
// tprintf("[DS] populating tmp GPUEnv from device\n");
24542454
populateGPUEnvFromDevice(env, device->oclDeviceID);
24552455
env->mnFileCount = 0; // argc;
24562456
env->mnKernelCount = 0UL;
2457-
// printf("[DS] compiling kernels for tmp GPUEnv\n");
2457+
// tprintf("[DS] compiling kernels for tmp GPUEnv\n");
24582458
OpenclDevice::CompileKernelFile(env, "");
24592459
}
24602460

@@ -2490,17 +2490,17 @@ static ds_status evaluateScoreForDevice(ds_device* device, void* inputData) {
24902490
device->score = new TessDeviceScore;
24912491
device->score->time = weightedTime;
24922492

2493-
printf("[DS] Device: \"%s\" (%s) evaluated\n", device->oclDeviceName,
2493+
tprintf("[DS] Device: \"%s\" (%s) evaluated\n", device->oclDeviceName,
24942494
device->type == DS_DEVICE_OPENCL_DEVICE ? "OpenCL" : "Native");
2495-
printf("[DS]%25s: %f (w=%.1f)\n", "composeRGBPixel", composeRGBPixelTime,
2495+
tprintf("[DS]%25s: %f (w=%.1f)\n", "composeRGBPixel", composeRGBPixelTime,
24962496
composeRGBPixelWeight);
2497-
printf("[DS]%25s: %f (w=%.1f)\n", "HistogramRect", histogramRectTime,
2497+
tprintf("[DS]%25s: %f (w=%.1f)\n", "HistogramRect", histogramRectTime,
24982498
histogramRectWeight);
2499-
printf("[DS]%25s: %f (w=%.1f)\n", "ThresholdRectToPix",
2499+
tprintf("[DS]%25s: %f (w=%.1f)\n", "ThresholdRectToPix",
25002500
thresholdRectToPixTime, thresholdRectToPixWeight);
2501-
printf("[DS]%25s: %f (w=%.1f)\n", "getLineMasksMorph", getLineMasksMorphTime,
2501+
tprintf("[DS]%25s: %f (w=%.1f)\n", "getLineMasksMorph", getLineMasksMorphTime,
25022502
getLineMasksMorphWeight);
2503-
printf("[DS]%25s: %f\n", "Score", device->score->time);
2503+
tprintf("[DS]%25s: %f\n", "Score", device->score->time);
25042504
return DS_SUCCESS;
25052505
}
25062506

@@ -2522,7 +2522,7 @@ ds_device OpenclDevice::getDeviceSelection() {
25222522
status = readProfileFromFile(profile, deserializeScore, fileName);
25232523
if (status != DS_SUCCESS) {
25242524
// need to run evaluation
2525-
printf("[DS] Profile file not available (%s); performing profiling.\n",
2525+
tprintf("[DS] Profile file not available (%s); performing profiling.\n",
25262526
fileName);
25272527

25282528
// create input data
@@ -2539,21 +2539,21 @@ ds_device OpenclDevice::getDeviceSelection() {
25392539
status = writeProfileToFile(profile, serializeScore, fileName);
25402540
PERF_COUNT_SUB("writeProfileToFile")
25412541
if (status == DS_SUCCESS) {
2542-
printf("[DS] Scores written to file (%s).\n", fileName);
2542+
tprintf("[DS] Scores written to file (%s).\n", fileName);
25432543
} else {
2544-
printf(
2544+
tprintf(
25452545
"[DS] Error saving scores to file (%s); scores not written to "
25462546
"file.\n",
25472547
fileName);
25482548
}
25492549
} else {
2550-
printf(
2550+
tprintf(
25512551
"[DS] Unable to evaluate performance; scores not written to "
25522552
"file.\n");
25532553
}
25542554
} else {
25552555
PERF_COUNT_SUB("readProfileFromFile")
2556-
printf("[DS] Profile read from file (%s).\n", fileName);
2556+
tprintf("[DS] Profile read from file (%s).\n", fileName);
25572557
}
25582558

25592559
// we now have device scores either from file or evaluation
@@ -2566,14 +2566,14 @@ ds_device OpenclDevice::getDeviceSelection() {
25662566
TessDeviceScore score = *device.score;
25672567

25682568
float time = score.time;
2569-
printf("[DS] Device[%u] %i:%s score is %f\n", d + 1, device.type,
2569+
tprintf("[DS] Device[%u] %i:%s score is %f\n", d + 1, device.type,
25702570
device.oclDeviceName, time);
25712571
if (time < bestTime) {
25722572
bestTime = time;
25732573
bestDeviceIdx = d;
25742574
}
25752575
}
2576-
printf("[DS] Selected Device[%i]: \"%s\" (%s)\n", bestDeviceIdx + 1,
2576+
tprintf("[DS] Selected Device[%i]: \"%s\" (%s)\n", bestDeviceIdx + 1,
25772577
profile->devices[bestDeviceIdx].oclDeviceName,
25782578
profile->devices[bestDeviceIdx].type == DS_DEVICE_OPENCL_DEVICE
25792579
? "OpenCL"
@@ -2586,22 +2586,22 @@ ds_device OpenclDevice::getDeviceSelection() {
25862586
if (overrideDeviceStr != nullptr) {
25872587
int overrideDeviceIdx = atoi(overrideDeviceStr);
25882588
if (overrideDeviceIdx > 0 && overrideDeviceIdx <= profile->numDevices) {
2589-
printf(
2589+
tprintf(
25902590
"[DS] Overriding Device Selection (TESSERACT_OPENCL_DEVICE=%s, "
25912591
"%i)\n",
25922592
overrideDeviceStr, overrideDeviceIdx);
25932593
bestDeviceIdx = overrideDeviceIdx - 1;
25942594
overridden = true;
25952595
} else {
2596-
printf(
2596+
tprintf(
25972597
"[DS] Ignoring invalid TESSERACT_OPENCL_DEVICE=%s ([1,%i] are "
25982598
"valid devices).\n",
25992599
overrideDeviceStr, profile->numDevices);
26002600
}
26012601
}
26022602

26032603
if (overridden) {
2604-
printf("[DS] Overridden Device[%i]: \"%s\" (%s)\n", bestDeviceIdx + 1,
2604+
tprintf("[DS] Overridden Device[%i]: \"%s\" (%s)\n", bestDeviceIdx + 1,
26052605
profile->devices[bestDeviceIdx].oclDeviceName,
26062606
profile->devices[bestDeviceIdx].type == DS_DEVICE_OPENCL_DEVICE
26072607
? "OpenCL"
@@ -2612,7 +2612,7 @@ ds_device OpenclDevice::getDeviceSelection() {
26122612
releaseDSProfile(profile, releaseScore);
26132613
} else {
26142614
// opencl isn't available at runtime, select native cpu device
2615-
printf("[DS] OpenCL runtime not available.\n");
2615+
tprintf("[DS] OpenCL runtime not available.\n");
26162616
selectedDevice.type = DS_DEVICE_NATIVE_CPU;
26172617
selectedDevice.oclDeviceName = "(null)";
26182618
selectedDevice.score = nullptr;

src/opencl/openclwrapper.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
QueryPerformanceCounter(&time_funct_end); \
9696
elapsed_time_sec = (time_funct_end.QuadPart - time_funct_start.QuadPart) / \
9797
(double)(freq.QuadPart); \
98-
printf(PERF_COUNT_REPORT_STR, funct_name, "total", elapsed_time_sec);
98+
tprintf(PERF_COUNT_REPORT_STR, funct_name, "total", elapsed_time_sec);
9999
#else
100100
#define PERF_COUNT_START(FUNCT_NAME)
101101
#define PERF_COUNT_END
@@ -106,7 +106,7 @@
106106
QueryPerformanceCounter(&time_sub_end); \
107107
elapsed_time_sec = (time_sub_end.QuadPart - time_sub_start.QuadPart) / \
108108
(double)(freq.QuadPart); \
109-
printf(PERF_COUNT_REPORT_STR, funct_name, SUB, elapsed_time_sec); \
109+
tprintf(PERF_COUNT_REPORT_STR, funct_name, SUB, elapsed_time_sec); \
110110
time_sub_start = time_sub_end;
111111
#else
112112
#define PERF_COUNT_SUB(SUB)
@@ -129,7 +129,7 @@
129129
elapsed_time_sec = \
130130
(time_funct_end.tv_sec - time_funct_start.tv_sec) * 1.0 + \
131131
(time_funct_end.tv_nsec - time_funct_start.tv_nsec) / 1000000000.0; \
132-
printf(PERF_COUNT_REPORT_STR, funct_name, "total", elapsed_time_sec);
132+
tprintf(PERF_COUNT_REPORT_STR, funct_name, "total", elapsed_time_sec);
133133
#else
134134
#define PERF_COUNT_START(FUNCT_NAME)
135135
#define PERF_COUNT_END
@@ -141,7 +141,7 @@
141141
elapsed_time_sec = \
142142
(time_sub_end.tv_sec - time_sub_start.tv_sec) * 1.0 + \
143143
(time_sub_end.tv_nsec - time_sub_start.tv_nsec) / 1000000000.0; \
144-
printf(PERF_COUNT_REPORT_STR, funct_name, SUB, elapsed_time_sec); \
144+
tprintf(PERF_COUNT_REPORT_STR, funct_name, SUB, elapsed_time_sec); \
145145
time_sub_start = time_sub_end;
146146
#else
147147
#define PERF_COUNT_SUB(SUB)
@@ -187,7 +187,7 @@ typedef int (*cl_kernel_function)(void** userdata, KernelEnv* kenv);
187187

188188
#define CHECK_OPENCL(status, name) \
189189
if (status != CL_SUCCESS) { \
190-
printf("OpenCL error code is %d at when %s .\n", status, name); \
190+
tprintf("OpenCL error code is %d at when %s .\n", status, name); \
191191
}
192192

193193
struct GPUEnv {

0 commit comments

Comments
 (0)