Skip to content

Commit

Permalink
Proper console log implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Segfaultd committed Dec 28, 2024
1 parent d0517d4 commit e54a893
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
36 changes: 36 additions & 0 deletions code/framework/src/scripting/builtins/console.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* MafiaHub OSS license
* Copyright (c) 2021-2023, MafiaHub. All rights reserved.
*
* This file comes from MafiaHub, hosted at https://github.com/MafiaHub/Framework.
* See LICENSE file in the source repository for information regarding licensing.
*/

#pragma once

#include <glm/glm.hpp>
#include <sol/sol.hpp>

#include <string>

namespace Framework::Scripting::Builtins {
class Console final {
static void Log(sol::variadic_args args) {
std::string str;

for (auto arg : args) {
// sol::variadic_args automatically converts arguments to strings
// when using string concatenation
str += arg.as<std::string>();
}

Framework::Logging::GetLogger(FRAMEWORK_INNER_SCRIPTING)->info(str);
}

public:
static void Register(sol::state &luaEngine) {
sol::usertype<Console> cls = luaEngine.new_usertype<Console>("Console");
cls["log"] = &Console::Log;
}
};
} // namespace Framework::Scripting::Builtins
19 changes: 2 additions & 17 deletions code/framework/src/scripting/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,14 @@

#include "engine.h"

#include "builtins/console.h"
#include "builtins/color_rgb.h"
#include "builtins/color_rgba.h"
#include "builtins/quaternion.h"
#include "builtins/vector_2.h"
#include "builtins/vector_3.h"

namespace Framework::Scripting {
int ConsoleLog(lua_State *L) {
int nargs = lua_gettop(L);

std::string str = "";

for (int i = 1; i <= nargs; ++i) {
str += luaL_tolstring(L, i, nullptr);
lua_pop(L, 1); // remove the string
}

Framework::Logging::GetLogger(FRAMEWORK_INNER_SCRIPTING)->info(str);

return 0;
}

/*static void On(const v8::FunctionCallbackInfo<v8::Value> &info) {
// Ensure that the method was called with exactly two arguments
if (info.Length() != 2) {
Expand Down Expand Up @@ -90,9 +76,8 @@ namespace Framework::Scripting {
bool Engine::InitCommonSDK() {
auto luaState = _luaEngine.lua_state();

_luaEngine["consoleLog"] = ConsoleLog;

// Bind the builtins
Builtins::Console::Register(_luaEngine);
Builtins::ColorRGB::Register(_luaEngine);
Builtins::ColorRGBA::Register(_luaEngine);
Builtins::Quaternion::Register(_luaEngine);
Expand Down

0 comments on commit e54a893

Please sign in to comment.