Skip to content

Commit

Permalink
lib: add user-defined logging
Browse files Browse the repository at this point in the history
  • Loading branch information
vaxerski committed Mar 24, 2024
1 parent f870f0f commit 22a4195
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 78 deletions.
19 changes: 18 additions & 1 deletion include/hyprcursor/hyprcursor.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ struct hyprcursor_cursor_style_info {
*/
CAPI struct hyprcursor_manager_t* hyprcursor_manager_create(const char* theme_name);

/*!
Same as hyprcursor_manager_create, but with a logger.
*/
CAPI struct hyprcursor_manager_t* hyprcursor_manager_create_with_logger(const char* theme_name, PHYPRCURSORLOGFUNC fn);

/*!
Free a hyprcursor_manager_t*
*/
Expand Down Expand Up @@ -71,7 +76,8 @@ CAPI int hyprcursor_load_theme_style(struct hyprcursor_manager_t* manager, struc
Once done with a size, call hyprcursor_style_done()
*/
CAPI hyprcursor_cursor_image_data** hyprcursor_get_cursor_image_data(struct hyprcursor_manager_t* manager, const char* shape, struct hyprcursor_cursor_style_info info, int* out_size);
CAPI hyprcursor_cursor_image_data** hyprcursor_get_cursor_image_data(struct hyprcursor_manager_t* manager, const char* shape, struct hyprcursor_cursor_style_info info,
int* out_size);

/*!
Free a returned hyprcursor_cursor_image_data.
Expand All @@ -83,4 +89,15 @@ CAPI void hyprcursor_cursor_image_data_free(hyprcursor_cursor_image_data** data,
*/
CAPI void hyprcursor_style_done(struct hyprcursor_manager_t* manager, struct hyprcursor_cursor_style_info info);

/*!
\since 0.1.5
Registers a logging function to a hyprcursor_manager_t*
PHYPRCURSORLOGFUNC's msg is owned by the caller and will be freed afterwards.
fn can be null to remove a logger.
*/
CAPI void hyprcursor_register_logging_function(struct hyprcursor_manager_t* manager, PHYPRCURSORLOGFUNC fn);

#endif
18 changes: 18 additions & 0 deletions include/hyprcursor/hyprcursor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ namespace Hyprcursor {
class CHyprcursorManager {
public:
CHyprcursorManager(const char* themeName);
/*!
\since 0.1.5
*/
CHyprcursorManager(const char* themeName, PHYPRCURSORLOGFUNC fn);
~CHyprcursorManager();

/*!
Expand Down Expand Up @@ -99,9 +103,23 @@ namespace Hyprcursor {
*/
void cursorSurfaceStyleDone(const SCursorStyleInfo&);

/*!
\since 0.1.5
Registers a logging function to this manager.
PHYPRCURSORLOGFUNC's msg is owned by the caller and will be freed afterwards.
fn can be null to unregister a logger.
*/
void registerLoggingFunction(PHYPRCURSORLOGFUNC fn);

private:
void init(const char* themeName_);

CHyprcursorImplementation* impl = nullptr;
bool finalizedAndValid = false;
PHYPRCURSORLOGFUNC logFn = nullptr;

friend class CHyprcursorImplementation;
};

}
14 changes: 14 additions & 0 deletions include/hyprcursor/shared.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,18 @@ struct SCursorImageData {

typedef struct SCursorImageData hyprcursor_cursor_image_data;

enum eHyprcursorLogLevel {
HC_LOG_NONE = 0,
HC_LOG_TRACE,
HC_LOG_INFO,
HC_LOG_WARN,
HC_LOG_ERR,
HC_LOG_CRITICAL,
};

/*
msg is owned by the caller and will be freed afterwards.
*/
typedef void (*PHYPRCURSORLOGFUNC)(enum eHyprcursorLogLevel level, char* msg);

#endif
43 changes: 6 additions & 37 deletions libhyprcursor/Log.hpp
Original file line number Diff line number Diff line change
@@ -1,53 +1,22 @@
#pragma once

enum eLogLevel {
TRACE = 0,
INFO,
LOG,
WARN,
ERR,
CRIT,
NONE
};

#include <string>
#include <format>
#include <iostream>

#include <hyprcursor/shared.h>

namespace Debug {
inline bool quiet = false;
inline bool verbose = false;

template <typename... Args>
void log(eLogLevel level, const std::string& fmt, Args&&... args) {

#ifndef HYPRLAND_DEBUG
// don't log in release
return;
#endif

if (!verbose && level == TRACE)
void log(eHyprcursorLogLevel level, PHYPRCURSORLOGFUNC fn, const std::string& fmt, Args&&... args) {
if (!fn)
return;

if (quiet)
return;

if (level != NONE) {
std::cout << '[';

switch (level) {
case TRACE: std::cout << "TRACE"; break;
case INFO: std::cout << "INFO"; break;
case LOG: std::cout << "LOG"; break;
case WARN: std::cout << "WARN"; break;
case ERR: std::cout << "ERR"; break;
case CRIT: std::cout << "CRITICAL"; break;
default: break;
}

std::cout << "] ";
}
const std::string LOG = std::vformat(fmt, std::make_format_args(args...));

std::cout << std::vformat(fmt, std::make_format_args(args...)) << "\n";
fn(level, (char*)LOG.c_str());
}
};
Loading

0 comments on commit 22a4195

Please sign in to comment.