Giter VIP home page Giter VIP logo

samp-logger's Introduction

samp-logger

This is a simple structured logger which provides a simple set of functions for logging and debugging.

Installation

Simply install to your project:

sampctl package install Southclaws/samp-logger

Include in your code and begin using the library:

#include <logger>

Usage

To log an event:

Logger_Log("something happened!");

To log an event with values to give the event more context:

Logger_Log("player changed world",
    Logger_S("name", playerName),
    Logger_I("worldid", 4));

This would be printed as:

text="player changed world" name="Southclaws" worldid=4

Which means you can easily read the log. The description of the event is isolated from the parameters instead of being mixed up in a sentence such as player Southclaws changed to world id 4. This also means the log is now very easy to parse automatically and process in some way.

Debugging

The library also includes a basic debug mechanism which uses the SVar system to facilitate named debug handlers which can be turned on and off at runtime.

The syntax is the same for Logger_Dbg as Logger_Log but with one extra parameter which describes the debug handler name:

Logger_ToggleDebug("weapons", true); // activate the "weapons" debug handler
Logger_Dbg("weapons", "player changed weapon",
    Logger_I("weaponid", 31));

Extending

The logging functions are just string concatenation functions. All Logger_S and friends do is format the string into a key=value format and simply return the string. So it's easy to write your own field converters:

stock Logger_W(const field[], weaponid) {
    new result[MAX_FIELD_NAME + 1 + MAX_FIELD_VALUE];
    new weaponName[32];
    GetWeaponName(weaponid, weaponName, 32);
    formatex(result, sizeof(result), "%s=\"%s\"", field, weaponName);
    return LOGGER_FIELD:result;
}

Development

To run tests, simply build and run the package:

sampctl package run --forceBuild --forceEnsure

samp-logger's People

Contributors

adrfranklin avatar denisbranisteanu avatar markkkkas avatar mergevos avatar patrickgtr avatar southclaws avatar spacemud avatar y-less avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

samp-logger's Issues

Argument type mismatch (argument 1)

I updated to 0.3.DL version, and got these errors.

D:\GAMEGTA FROM ZERO\dependencies\samp-logger\logger.inc:88 (error) argument type mismatch (argument 1)
D:\GAMEGTA FROM ZERO\dependencies\samp-logger\logger.inc:180 (error) argument type mismatch (argument 1)
D:\GAMEGTA FROM ZERO\dependencies\samp-logger\logger.inc:182 (error) argument type mismatch (argument 1)
if(GetSVarInt(handler) == 0) { //err line
	return;
}
stock logger_debug(const handler[], bool:toggle) {
	if(toggle) {
		SetSVarInt(handler, 1); //err line
	} else {
		DeleteSVar(handler); //err line
	}
	return 1;
}

Function renaming proposal

I wish to propose a function name change, due to the current naming scheme it causes a lot of problems with other libraries, libraries that use common names like err, dbg, and log.

So I want to propose that we instead change the functions to a proper naming scheme, so instead of a function like err we would now use it like Logger_Err and for the types that are used, we instead use Logger_I instead of _i. This adds a lot more clarity to what package this function belongs to, and escapes the possibility of causing warnings for the end user.

Here I will provide an example from the readme file that shows the current and the new proposed changes.

current:

log("something happened!");

logger_debug("weapons", true); // activate the "weapons" debug handler
dbg("weapons", "player changed weapon",
    _i("weaponid", 31));

new changes:

Logger_Log("something happened!");

Logger_Debug("weapons", true); // activate the "weapons" debug handler
Logger_Dbg("weapons", "player changed weapon",
    Logger_I("weaponid", 31));

I would also like the propose the ability to define a compatibility definition, that would toggle between the old functions and the new ones. This way people who are currently happy using the library as is, don't need to be forced to make changes to their gamemode, should they decide to upgrade past this proposal.

The proposed compatibility definition would be called SAMP_LOGGER_COMPAT which would mean that current library users would still need to define SAMP_LOGGER_COMPAT to continue using the old version of the functions.

Memory allocation problem

Calling functions in each other causes Stack/heap collision
That logger has likewise problem -> if _s, _i, _f are called several times inside log(), Run time error 3: "Stack/heap collision (insufficient stack size)" comes up.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.