Add -q/--quiet flag to suppress info logs

This commit is contained in:
wangjue 2026-05-29 16:17:34 +08:00
parent cf7712830b
commit d8edd92302

View File

@ -8,6 +8,7 @@
#include <csignal> #include <csignal>
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <cstring>
#include <getopt.h> #include <getopt.h>
#include <string> #include <string>
#include <sys/stat.h> #include <sys/stat.h>
@ -17,6 +18,10 @@
static volatile bool g_interrupted = false; static volatile bool g_interrupted = false;
static void noop_log(enum ggml_log_level level, const char * text, void * user_data) {
(void)level; (void)text; (void)user_data;
}
static void sigint_handler(const int signo) { static void sigint_handler(const int signo) {
(void)signo; (void)signo;
g_interrupted = true; g_interrupted = true;
@ -113,13 +118,13 @@ static std::vector<unsigned char> read_clipboard() {
return data; return data;
} }
static std::string capture_screenshot() { static std::string capture_screenshot(bool quiet) {
const char *tmpdir = getenv("TMPDIR"); const char *tmpdir = getenv("TMPDIR");
if (!tmpdir) if (!tmpdir)
tmpdir = "/tmp"; tmpdir = "/tmp";
std::string path = std::string(tmpdir) + "/ocr_screenshot.png"; std::string path = std::string(tmpdir) + "/ocr_screenshot.png";
fprintf(stderr, "Select a screen region to OCR...\n"); if (!quiet) fprintf(stderr, "Select a screen region to OCR...\n");
auto check = [&]() -> bool { auto check = [&]() -> bool {
struct stat st {}; struct stat st {};
@ -178,6 +183,7 @@ static void print_usage(const char *prog) {
" --json output in JSON format\n" " --json output in JSON format\n"
" --chat-template <name> chat template (default: auto from model)\n" " --chat-template <name> chat template (default: auto from model)\n"
" --screenshot interactively select screen region to OCR\n" " --screenshot interactively select screen region to OCR\n"
" -q, --quiet suppress info logs, show only OCR result\n"
" -h show this help\n" " -h show this help\n"
"\n" "\n"
"If <image_path> is omitted, reads image from clipboard.\n" "If <image_path> is omitted, reads image from clipboard.\n"
@ -204,6 +210,14 @@ int main(int argc, char **argv) {
float temp = 0.0f; float temp = 0.0f;
uint32_t seed = LLAMA_DEFAULT_SEED; uint32_t seed = LLAMA_DEFAULT_SEED;
bool json_output = false; bool json_output = false;
bool quiet = false;
// pre-process -mm to --mmproj so getopt handles it correctly
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "-mm") == 0) {
argv[i] = (char *)"--mmproj";
}
}
while (true) { while (true) {
static option long_opts[] = { static option long_opts[] = {
@ -215,10 +229,11 @@ int main(int argc, char **argv) {
{"model", required_argument, nullptr, 'm'}, {"model", required_argument, nullptr, 'm'},
{"chat-template", required_argument, nullptr, 4}, {"chat-template", required_argument, nullptr, 4},
{"screenshot", no_argument, nullptr, 5}, {"screenshot", no_argument, nullptr, 5},
{"quiet", no_argument, nullptr, 6},
{"help", no_argument, nullptr, 'h'}, {"help", no_argument, nullptr, 'h'},
{nullptr, 0, nullptr, 0}}; {nullptr, 0, nullptr, 0}};
int idx = 0; int idx = 0;
int c = getopt_long(argc, argv, "m:p:t:c:s:h", long_opts, &idx); int c = getopt_long(argc, argv, "m:p:t:c:s:qh", long_opts, &idx);
if (c == -1) if (c == -1)
break; break;
switch (c) { switch (c) {
@ -240,6 +255,10 @@ int main(int argc, char **argv) {
case 5: case 5:
screenshot_mode = true; screenshot_mode = true;
break; break;
case 6:
case 'q':
quiet = true;
break;
case 'm': case 'm':
model_path = optarg; model_path = optarg;
break; break;
@ -270,7 +289,7 @@ int main(int argc, char **argv) {
} }
if (screenshot_mode) { if (screenshot_mode) {
image_path = capture_screenshot(); image_path = capture_screenshot(quiet);
if (image_path.empty()) { if (image_path.empty()) {
fprintf(stderr, fprintf(stderr,
"ERROR: no screenshot tool found. Install gnome-screenshot, " "ERROR: no screenshot tool found. Install gnome-screenshot, "
@ -287,6 +306,11 @@ int main(int argc, char **argv) {
llama_backend_init(); llama_backend_init();
if (quiet) {
llama_log_set(noop_log, nullptr);
mtmd_helper_log_set(noop_log, nullptr);
}
llama_model_params mparams = llama_model_default_params(); llama_model_params mparams = llama_model_default_params();
mparams.n_gpu_layers = n_gpu_layers; mparams.n_gpu_layers = n_gpu_layers;
@ -343,7 +367,7 @@ int main(int argc, char **argv) {
return 1; return 1;
} }
} else { } else {
fprintf(stderr, "Reading image from clipboard...\n"); if (!quiet) fprintf(stderr, "Reading image from clipboard...\n");
auto clip = read_clipboard(); auto clip = read_clipboard();
if (clip.empty()) { if (clip.empty()) {
fprintf(stderr, fprintf(stderr,