Skip to content
Snippets Groups Projects
Unverified Commit 7941343c authored by Johannes Marbach's avatar Johannes Marbach Committed by Clayton Craft
Browse files

Replace printf with SDL_Log variants (MR 108)

All usages of printf were migrated to SDL_Log functions. This allows
the log verbosity to be controlled centrally and the -v flag is only
used once for setting the global priority threshold.

Closes: #107
parent fb8dd4bc
No related branches found
No related tags found
1 merge request!108Replace printf with SDL_Log variants
Pipeline #186342 passed
......@@ -18,6 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <SDL2/SDL.h>
#include <fstream>
#include <iostream>
#include <map>
......@@ -28,11 +29,11 @@ bool Config::Read(const std::string &path)
{
std::ifstream is(path, std::ifstream::binary);
if (!is) {
fprintf(stderr, "Could not open config file: %s\n", path.c_str());
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Could not open config file: %s", path.c_str());
return false;
}
if (!Config::Parse(is)) {
fprintf(stderr, "Could not parse config file: %s\n", path.c_str());
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Could not parse config file: %s", path.c_str());
return false;
}
......@@ -95,7 +96,7 @@ bool Config::Parse(std::istream &file)
}
if (error) {
fprintf(stderr, "Syntax error on line %d\n", lineno);
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Syntax error on line %d", lineno);
return false;
} else {
Config::options[id] = val;
......
......@@ -36,7 +36,7 @@ int Keyboard::init(SDL_Renderer *renderer)
loadKeymap();
int keyLong = std::strtol(config->keyRadius.c_str(), nullptr, 10);
if (keyLong >= BEZIER_RESOLUTION || static_cast<double>(keyLong) > (keyboardHeight / 5.0) / 1.5) {
fprintf(stderr, "key-radius must be below %d and %f, it is %d\n",
SDL_LogWarn(SDL_LOG_CATEGORY_VIDEO, "key-radius must be below %d and %f, it is %d",
BEZIER_RESOLUTION, (keyboardHeight / 5.0) / 1.5, keyLong);
keyRadius = 0;
} else {
......@@ -45,12 +45,12 @@ int Keyboard::init(SDL_Renderer *renderer)
for (auto &layer : keyboard) {
layer.surface = makeKeyboard(&layer);
if (!layer.surface) {
fprintf(stderr, "ERROR: Unable to generate keyboard surface\n");
SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "Unable to generate keyboard surface");
return 1;
}
layer.texture = SDL_CreateTextureFromSurface(renderer, layer.surface);
if (!layer.texture) {
fprintf(stderr, "ERROR: Unable to generate keyboard texture\n");
SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "Unable to generate keyboard texture");
return 1;
}
}
......@@ -230,7 +230,7 @@ SDL_Surface *Keyboard::makeKeyboard(KeyboardLayer *layer) const
bmask, amask);
if (surface == nullptr) {
fprintf(stderr, "CreateRGBSurface failed: %s\n", SDL_GetError());
SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "CreateRGBSurface failed: %s", SDL_GetError());
return nullptr;
}
......@@ -241,13 +241,13 @@ SDL_Surface *Keyboard::makeKeyboard(KeyboardLayer *layer) const
int rowHeight = keyboardHeight / (rowCount + 1);
if (TTF_Init() == -1) {
printf("TTF_Init: %s\n", TTF_GetError());
SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "TTF_Init: %s", TTF_GetError());
return nullptr;
}
TTF_Font *font = TTF_OpenFont(config->keyboardFont.c_str(), 24);
if (!font) {
printf("TTF_OpenFont: %s\n", TTF_GetError());
SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "TTF_OpenFont: %s", TTF_GetError());
return nullptr;
}
......@@ -331,7 +331,7 @@ void Keyboard::setActiveLayer(int layerNum)
return;
}
}
fprintf(stderr, "Unknown layer number: %i\n", layerNum);
SDL_LogWarn(SDL_LOG_CATEGORY_ERROR, "Unknown layer number: %i", layerNum);
}
/*
......
......@@ -40,7 +40,7 @@ int LuksDevice::unlock(void *luksDev)
// Initialize crypt device
ret = crypt_init(&cd, lcd->devicePath.c_str());
if (ret < 0) {
printf("crypt_init() failed for %s.\n", lcd->devicePath.c_str());
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "crypt_init() failed for %s.", lcd->devicePath.c_str());
lcd->running = false;
return ret;
}
......@@ -48,7 +48,7 @@ int LuksDevice::unlock(void *luksDev)
// Load header
ret = crypt_load(cd, nullptr, nullptr);
if (ret < 0) {
printf("crypt_load() failed on device %s.\n", crypt_get_device_name(cd));
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "crypt_load() failed on device %s.", crypt_get_device_name(cd));
crypt_free(cd);
lcd->running = false;
return ret;
......@@ -62,12 +62,12 @@ int LuksDevice::unlock(void *luksDev)
// Enable TRIM support
CRYPT_ACTIVATE_ALLOW_DISCARDS);
if (ret < 0) {
printf("crypt_activate_by_passphrase failed on device. Errno %i\n", ret);
SDL_Log("crypt_activate_by_passphrase failed on device. Errno %i", ret);
crypt_free(cd);
lcd->running = false;
return ret;
}
printf("Successfully unlocked device %s\n", lcd->devicePath.c_str());
SDL_Log("Successfully unlocked device %s", lcd->devicePath.c_str());
crypt_free(cd);
lcd->locked = false;
lcd->running = false;
......
......@@ -55,23 +55,24 @@ int main(int argc, char **args)
.type = EVENT_RENDER
};
SDL_LogSetAllPriority(SDL_LOG_PRIORITY_ERROR);
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
if (fetchOpts(argc, args, &opts)) {
exit(EXIT_FAILURE);
}
if (opts.verbose) {
SDL_LogSetAllPriority(SDL_LOG_PRIORITY_INFO);
}
if (!config.Read(opts.confPath)) {
fprintf(stderr, "No valid config file specified, use -c [path]\n");
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "No valid config file specified, use -c [path]");
exit(EXIT_FAILURE);
}
LuksDevice luksDev(opts.luksDevName, opts.luksDevPath);
if (opts.verbose) {
SDL_LogSetAllPriority(SDL_LOG_PRIORITY_INFO);
} else {
SDL_LogSetAllPriority(SDL_LOG_PRIORITY_ERROR);
}
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS | SDL_INIT_TIMER) < 0) {
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "SDL_Init failed: %s", SDL_GetError());
atexit(SDL_Quit);
......@@ -105,7 +106,7 @@ int main(int argc, char **args)
display = SDL_CreateWindow("OSK SDL", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT,
windowFlags);
if (display == nullptr) {
fprintf(stderr, "ERROR: Could not create window/display: %s\n", SDL_GetError());
SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "Could not create window/display: %s", SDL_GetError());
atexit(SDL_Quit);
exit(EXIT_FAILURE);
}
......@@ -113,7 +114,7 @@ int main(int argc, char **args)
renderer = SDL_CreateRenderer(display, -1, 0);
if (renderer == nullptr) {
SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "ERROR: Could not create renderer: %s\n", SDL_GetError());
SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "Could not create renderer: %s", SDL_GetError());
atexit(SDL_Quit);
exit(EXIT_FAILURE);
}
......@@ -127,13 +128,13 @@ int main(int argc, char **args)
int inputHeight = WIDTH / 10;
if (SDL_SetRenderDrawColor(renderer, 255, 128, 0, SDL_ALPHA_OPAQUE) != 0) {
SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "ERROR: Could not set background color: %s\n", SDL_GetError());
SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "Could not set background color: %s", SDL_GetError());
atexit(SDL_Quit);
exit(EXIT_FAILURE);
}
if (SDL_RenderFillRect(renderer, nullptr) != 0) {
SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "ERROR: Could not fill background color: %s\n", SDL_GetError());
SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "Could not fill background color: %s", SDL_GetError());
atexit(SDL_Quit);
exit(EXIT_FAILURE);
}
......@@ -142,18 +143,22 @@ int main(int argc, char **args)
Keyboard keyboard(0, 1, WIDTH, keyboardHeight, &config);
keyboard.setKeyboardColor(0, 30, 30, 30);
if (keyboard.init(renderer)) {
fprintf(stderr, "ERROR: Failed to initialize keyboard!\n");
SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "Failed to initialize keyboard!");
atexit(SDL_Quit);
exit(EXIT_FAILURE);
}
// Initialize tooltip for password error
Tooltip tooltip(static_cast<int>(WIDTH * 0.9), inputHeight, &config);
tooltip.init(renderer, ErrorText);
if (tooltip.init(renderer, ErrorText)) {
SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "Failed to initialize tooltip!");
atexit(SDL_Quit);
exit(EXIT_FAILURE);
}
// Disable mouse cursor if not in testmode
if (SDL_ShowCursor(opts.testMode) < 0) {
fprintf(stderr, "Setting cursor visibility failed: %s\n", SDL_GetError());
SDL_LogWarn(SDL_LOG_CATEGORY_VIDEO, "Setting cursor visibility failed: %s", SDL_GetError());
// Not stopping here, this is a pretty recoverable error.
}
......@@ -166,7 +171,7 @@ int main(int argc, char **args)
std::string tapped;
int inputBoxRadius = std::strtol(config.inputBoxRadius.c_str(), nullptr, 10);
if (inputBoxRadius >= BEZIER_RESOLUTION || inputBoxRadius > inputHeight / 1.5) {
fprintf(stderr, "inputbox-radius must be below %d and %f, it is %d\n", BEZIER_RESOLUTION,
SDL_LogWarn(SDL_LOG_CATEGORY_VIDEO, "inputbox-radius must be below %d and %f, it is %d", BEZIER_RESOLUTION,
inputHeight / 1.5, inputBoxRadius);
inputBoxRadius = 0;
}
......@@ -175,7 +180,7 @@ int main(int argc, char **args)
if (sscanf(config.wallpaper.c_str(), "#%02hhx%02hhx%02hhx", &wallpaperColor.r, &wallpaperColor.g,
&wallpaperColor.b)
!= 3) {
fprintf(stderr, "Could not parse color code %s\n", config.wallpaper.c_str());
SDL_LogWarn(SDL_LOG_CATEGORY_ERROR, "Could not parse color code %s", config.wallpaper.c_str());
//to avoid akward colors just remove the radius
inputBoxRadius = 0;
}
......@@ -194,7 +199,7 @@ int main(int argc, char **args)
};
if (inputBoxTexture == nullptr) {
SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "ERROR: Could not create input box texture: %s\n",
SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "Could not create input box texture: %s",
SDL_GetError());
atexit(SDL_Quit);
exit(EXIT_FAILURE);
......@@ -252,9 +257,7 @@ int main(int argc, char **args)
// x and y values are normalized!
auto xTouch = static_cast<unsigned>(event.tfinger.x * WIDTH);
auto yTouch = static_cast<unsigned>(event.tfinger.y * HEIGHT);
if (opts.verbose) {
printf("xTouch: %u\tyTouch: %u\n", xTouch, yTouch);
}
SDL_LogInfo(SDL_LOG_CATEGORY_VIDEO, "xTouch: %u\tyTouch: %u", xTouch, yTouch);
auto offsetYTouch = yTouch - static_cast<int>(HEIGHT - (keyboard.getHeight() * keyboard.getPosition()));
tapped = keyboard.getCharForCoordinates(xTouch, offsetYTouch);
if (!luksDev.unlockRunning()) {
......@@ -268,9 +271,7 @@ int main(int argc, char **args)
showPasswordError = false;
auto xMouse = event.button.x;
auto yMouse = event.button.y;
if (opts.verbose) {
printf("xMouse: %u\tyMouse: %u\n", xMouse, yMouse);
}
SDL_LogInfo(SDL_LOG_CATEGORY_VIDEO, "xMouse: %u\tyMouse: %u", xMouse, yMouse);
auto offsetYMouse = yMouse - static_cast<int>(HEIGHT - (keyboard.getHeight() * keyboard.getPosition()));
tapped = keyboard.getCharForCoordinates(xMouse, offsetYMouse);
if (!luksDev.unlockRunning()) {
......@@ -291,16 +292,14 @@ int main(int argc, char **args)
prev_text_ticks = cur_ticks;
if (!luksDev.unlockRunning()) {
passphrase.emplace_back(event.text.text);
if (opts.verbose) {
printf("Phys Keyboard Key Entered %s\n", event.text.text);
}
SDL_LogInfo(SDL_LOG_CATEGORY_INPUT, "Phys Keyboard Key Entered %s", event.text.text);
}
}
SDL_PushEvent(&renderEvent);
break; // SDL_TEXTINPUT
}
case SDL_QUIT:
printf("Quit requested, quitting.\n");
SDL_Log("Quit requested, quitting.");
exit(0);
break; // SDL_QUIT
} // switch event.type
......@@ -377,8 +376,7 @@ QUIT:
if (opts.keyscript) {
std::string pass = strVector2str(passphrase);
printf("%s", pass.c_str());
fflush(stdout);
SDL_LogInfo(SDL_LOG_CATEGORY_INPUT, "%s", pass.c_str());
}
return 0;
}
......@@ -46,7 +46,7 @@ int Tooltip::init(SDL_Renderer *renderer, const std::string &text)
surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32, rmask, gmask,
bmask, amask);
if (surface == nullptr) {
fprintf(stderr, "CreateRGBSurface failed: %s\n", SDL_GetError());
SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "CreateRGBSurface failed: %s", SDL_GetError());
return -1;
}
SDL_FillRect(surface, nullptr, SDL_MapRGB(surface->format, 30, 30, 30));
......
......@@ -48,16 +48,16 @@ int fetchOpts(int argc, char **args, Opts *opts)
opts->verbose = true;
break;
default:
fprintf(stdout, "Usage: osk-sdl [-t] [-k] [-d /dev/sda] [-n device_name] "
"[-c /etc/osk.conf] -v\n");
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Usage: osk-sdl [-t] [-k] [-d /dev/sda] [-n device_name] "
"[-c /etc/osk.conf] -v");
return 1;
}
if (opts->luksDevPath.empty()) {
fprintf(stderr, "No device path specified, use -d [path] or -t\n");
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "No device path specified, use -d [path] or -t");
return 1;
}
if (opts->luksDevName.empty()) {
fprintf(stderr, "No device name specified, use -n [name] or -t\n");
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "No device name specified, use -n [name] or -t");
return 1;
}
if (opts->confPath.empty()) {
......@@ -102,13 +102,13 @@ SDL_Surface *make_wallpaper(Config *config, int width, int height)
if (config->wallpaper[0] == '#') {
unsigned char r, g, b;
if (sscanf(config->wallpaper.c_str(), "#%02hhx%02hhx%02hhx", &r, &g, &b) != 3) {
fprintf(stderr, "Could not parse color code %s\n", config->wallpaper.c_str());
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Could not parse color code %s", config->wallpaper.c_str());
exit(EXIT_FAILURE);
}
SDL_FillRect(surface, nullptr, SDL_MapRGB(surface->format, r, g, b));
} else {
// Implement image loading
fprintf(stderr, "Image loading not supported yet\n");
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Image loading not supported yet");
exit(EXIT_FAILURE);
}
return surface;
......
  • Administrator @root

    mentioned in issue #120 (closed)

    By Undef on 2021-01-12T08:24:10

    · Imported

    mentioned in issue #120 (closed)

    By Undef on 2021-01-12T08:24:10

    Edited by Ghost User
    Toggle commit list
  • Administrator @root

    mentioned in merge request !117 (merged)

    By Johannes Marbach on 2021-01-12T18:32:18

    · Imported

    mentioned in merge request !117 (merged)

    By Johannes Marbach on 2021-01-12T18:32:18

    Edited by Ghost User
    Toggle commit list
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment