Skip to content

Commit

Permalink
Improve logging to display Lua location (instead of C++ location)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarod42 committed Aug 13, 2024
1 parent c75316d commit 879c6cf
Show file tree
Hide file tree
Showing 15 changed files with 129 additions and 104 deletions.
13 changes: 7 additions & 6 deletions src/ai/script_ai.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,9 +309,10 @@ static int CclDefineAiHelper(lua_State *l)
|| value == "research"
|| value == "unit-limit"
|| value == "repair") {
DebugPrint("DefineAiHelper: Relation is computed from buttons, "
"you may remove safely the block beginning with '\"%s\"'\n",
value.data());
LuaDebugPrint(l,
"DefineAiHelper: Relation is computed from buttons, "
"you may remove safely the block beginning with '\"%s\"'\n",
value.data());
continue;
} else if (value == "unit-equiv") {
} else {
Expand Down Expand Up @@ -397,7 +398,7 @@ static int CclDefineAi(lua_State *l)

#ifdef DEBUG
if (GetAiTypesByName(aiName)) {
DebugPrint("Warning two or more AI's with the same name '%s'\n", aiName.data());
LuaDebugPrint(l, "Warning two or more AI's with the same name '%s'\n", aiName.data());
}
#endif
AiTypes.insert(AiTypes.begin(), std::make_unique<CAiType>());
Expand Down Expand Up @@ -741,7 +742,7 @@ static int CclAiWait(lua_State *l)
lua_pushboolean(l, 1);
return 1;
}
DebugPrint("Broken? waiting on %s which wasn't requested.\n", type->Ident.c_str());
LuaDebugPrint(l, "Broken? waiting on %s which wasn't requested.\n", type->Ident.c_str());
lua_pushboolean(l, 0);
return 1;
}
Expand Down Expand Up @@ -1606,7 +1607,7 @@ static int CclDefineAiPlayer(lua_State *l)
const unsigned int playerIdx = LuaToNumber(l, 0 + 1);

Assert(playerIdx <= PlayerMax);
DebugPrint("%p %d\n", (void *)Players[playerIdx].Ai.get(), Players[playerIdx].AiEnabled);
LuaDebugPrint(l, "%p %d\n", (void *)Players[playerIdx].Ai.get(), Players[playerIdx].AiEnabled);
// FIXME: lose this:
// Assert(!Players[playerIdx].Ai && Players[playerIdx].AiEnabled);

Expand Down
2 changes: 1 addition & 1 deletion src/game/trigger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ static int CclAddTrigger(lua_State *l)
lua_getglobal(l, "_triggers_");

if (lua_isnil(l, -1)) {
DebugPrint("Trigger not set, defining trigger\n");
LuaDebugPrint(l, "Trigger not set, defining trigger\n");
lua_pop(l, 1);
lua_newtable(l);
lua_setglobal(l, "_triggers_");
Expand Down
10 changes: 10 additions & 0 deletions src/include/script.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ extern int LuaLoadFile(const fs::path &file, const std::string &strArg = "", boo
extern int LuaCall(int narg, int clear, bool exitOnError = true);
extern int LuaCall(lua_State *L, int narg, int nresults, int base, bool exitOnError = true);

std::string getLuaLocation(lua_State *l);

#define LuaDebugPrint(l, format, ...) \
do { \
if (EnableDebugPrint) { \
PrintOnStdOut(getLuaLocation(l)); \
PrintOnStdOut(Format(format, ##__VA_ARGS__)); \
} \
} while (0)

#define LuaError(l, format, ...) \
do { \
PrintFunction(); \
Expand Down
1 change: 0 additions & 1 deletion src/include/stratagus.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ inline void PrintOnStdOut(std::string_view s)
*/
#define LogPrint(format, ...) \
do { \
PrintFunction(); \
PrintOnStdOut(Format(format, ##__VA_ARGS__)); \
} while (0)

Expand Down
118 changes: 62 additions & 56 deletions src/map/script_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ static int CclStratagusMap(lua_State *l)
}
const int subsubargs = lua_rawlen(l, -1);
if (subsubargs != Map.Info.MapWidth * Map.Info.MapHeight) {
ErrorPrint("Wrong tile table length: %d\n", subsubargs);
LuaError(l, "Wrong tile table length: %d\n", subsubargs);
}
for (int i = 0; i < subsubargs; ++i) {
lua_rawgeti(l, -1, i + 1);
Expand Down Expand Up @@ -149,9 +149,10 @@ static int CclRevealMap(lua_State *l)
} else if (revealMode == "explored") {
newMode = MapRevealModes::cExplored;
} else {
ErrorPrint("Unknown reveal mode: '%s'\n"
"Accessible reveal modes: \"hidden\", \"known\", \"explored\".\n",
revealMode.data());
LuaError(l,
"Unknown reveal mode: '%s'\n"
"Accessible reveal modes: \"hidden\", \"known\", \"explored\".\n",
revealMode.data());
return 1;
}

Expand Down Expand Up @@ -233,7 +234,7 @@ static int CclShowMapLocation(lua_State *l)
target->CurrentSightRange = LuaToNumber(l, 3);
MapMarkUnitSight(*target);
} else {
DebugPrint("Unable to allocate Unit");
LuaDebugPrint(l, "Unable to allocate Unit");
}
return 0;
}
Expand Down Expand Up @@ -369,9 +370,10 @@ static int CclSetFieldOfViewType(lua_State *l)
} else if (type_name == "simple-radial") {
new_type = FieldOfViewTypes::cSimpleRadial;
} else {
ErrorPrint("Unknown field of view types '%s'\n"
"Accessible field of view types: \"shadow-casting\", \"simple-radial\".\n",
type_name.data());
LuaError(l,
"Unknown field of view types '%s'\n"
"Accessible field of view types: \"shadow-casting\", \"simple-radial\".\n",
type_name.data());
return 1;
}
if (!IsNetworkGame()) {
Expand Down Expand Up @@ -416,9 +418,10 @@ static int CclSetOpaqueFor(lua_State *l)
} else if (flag_name == "forest") {
new_flag |= MapFieldForest;
} else {
ErrorPrint("Opaque can only be set for \"wall\", \"rock\" or \"forest\".\n"
"You use '%s'\n",
flag_name.data());
LuaError(l,
"Opaque can only be set for \"wall\", \"rock\" or \"forest\".\n"
"You use '%s'\n",
flag_name.data());
return 1;
}
}
Expand Down Expand Up @@ -449,9 +452,10 @@ static int CclGetIsOpaqueFor(lua_State *l)
} else if (flag_name == "forest") {
flagToCheck = MapFieldForest;
} else {
ErrorPrint("Opaque can only be checked for \"wall\", \"rock\" or \"forest\".\n"
"You use '%s'\n",
flag_name.data());
LuaError(l,
"Opaque can only be checked for \"wall\", \"rock\" or \"forest\".\n"
"You use '%s'\n",
flag_name.data());
}

lua_pushboolean(l, FieldOfView.GetOpaqueFields() & flagToCheck);
Expand All @@ -475,9 +479,10 @@ static int CclRemoveOpaqueFor(lua_State *l)
} else if (flag_name == "forest") {
new_flag |= MapFieldForest;
} else {
ErrorPrint("Opaque can only be removed for \"wall\", \"rock\" or \"forest\".\n"
"You use '%s'\n",
flag_name.data());
LuaError(l,
"Opaque can only be removed for \"wall\", \"rock\" or \"forest\".\n"
"You use '%s'\n",
flag_name.data());
return 1;
}
}
Expand Down Expand Up @@ -518,9 +523,10 @@ static int CclSetFogOfWarType(lua_State *l)
} else if (type_name == "enhanced") {
new_type = FogOfWarTypes::cEnhanced;
} else {
ErrorPrint("Unknown fog of war type '%s'\n"
"Accessible Fog of War types: \"tiled\", \"enhanced\" and \"fast\".\n",
type_name.data());
LuaError(l,
"Unknown fog of war type '%s'\n"
"Accessible Fog of War types: \"tiled\", \"enhanced\" and \"fast\".\n",
type_name.data());
return 1;
}
FogOfWar->SetType(new_type);
Expand All @@ -542,34 +548,34 @@ static int CclGetFogOfWarType(lua_State *l)
**
** @param l Lua state.
**
** @return 0 for success, 1 for wrong type;
** @return 0
*/
static int CclSetFogOfWarOpacityLevels(lua_State *l)
{
LuaCheckArgs(l, 3);
const int explored = LuaToNumber(l, 1);
if (explored <= 0 || explored > 255) {
PrintFunction();
ErrorPrint("Invalid value (%d) of opacity for Explored tiles. "
"Acceptable range is [0 <= Explored <= Known <= Hidden <= 255].\n",
explored);
return 1;
LuaError(l,
"Invalid value (%d) of opacity for Explored tiles. "
"Acceptable range is [0 <= Explored <= Known <= Hidden <= 255].\n",
explored);
return 0;
}
const int revealed = LuaToNumber(l, 2);
if (revealed <= explored || revealed > 255) {
PrintFunction();
ErrorPrint("Invalid value (%d) of opacity for Revealed tiles. "
"Acceptable range is [0 <= Explored <= Known <= Hidden <= 255].\n",
revealed);
return 1;
LuaError(l,
"Invalid value (%d) of opacity for Revealed tiles. "
"Acceptable range is [0 <= Explored <= Known <= Hidden <= 255].\n",
revealed);
return 0;
}
const int unseen = LuaToNumber(l, 3);
if (unseen < revealed || unseen > 255) {
PrintFunction();
ErrorPrint("Invalid value (%d) of opacity for Unseen tiles. "
"Acceptable range is [0 <= Explored <= Known <= Hidden <= 255].\n",
unseen);
return 1;
LuaError(l,
"Invalid value (%d) of opacity for Unseen tiles. "
"Acceptable range is [0 <= Explored <= Known <= Hidden <= 255].\n",
unseen);
return 0;
}

FogOfWar->SetOpacityLevels(explored, revealed, unseen);
Expand All @@ -590,17 +596,17 @@ static int CclSetFogOfWarBlur(lua_State *l)

const float radiusSimple = LuaToFloat(l, 1);
if (radiusSimple <= 0 ) {
ErrorPrint("Radius should be a positive float number. Blur is disabled.\n");
LuaError(l, "Radius should be a positive float number. Blur is disabled.\n");
}

const float radiusBilinear = LuaToFloat(l, 2);
if (radiusBilinear <= 0 ) {
ErrorPrint("Radius should be a positive float number. Blur is disabled.\n");
LuaError(l, "Radius should be a positive float number. Blur is disabled.\n");
}

const int iterations = LuaToNumber(l, 3);
if (iterations <= 0 ) {
ErrorPrint("Number of box blur iterations should be greater than 0. Blur is disabled.\n");
LuaError(l, "Number of box blur iterations should be greater than 0. Blur is disabled.\n");
}
FogOfWar->InitBlurer(radiusSimple, radiusBilinear, iterations);
return 0;
Expand Down Expand Up @@ -735,34 +741,34 @@ static int CclSetFogOfWarGraphics(lua_State *l)
**
** @param l Lua state.
**
** @return 0 for success, 1 for wrong type;
** @return 0
*/
static int CclSetMMFogOfWarOpacityLevels(lua_State *l)
{
LuaCheckArgs(l, 3);
const int explored = LuaToNumber(l, 1);
if (explored <= 0 || explored > 255) {
PrintFunction();
ErrorPrint("Invalid value (%d) of opacity for Minimap's Explored tiles. "
"Acceptable range is [0 <= Explored <= Known <= Hidden <= 255].\n",
explored);
return 1;
LuaError(l,
"Invalid value (%d) of opacity for Minimap's Explored tiles. "
"Acceptable range is [0 <= Explored <= Known <= Hidden <= 255].\n",
explored);
return 0;
}
const int revealed = LuaToNumber(l, 2);
if (revealed <= explored || revealed > 255) {
PrintFunction();
ErrorPrint("Invalid value (%d) of opacity for Minimap's Revealed tiles. "
"Acceptable range is [0 <= Explored <= Known <= Hidden <= 255].\n",
revealed);
return 1;
LuaError(l,
"Invalid value (%d) of opacity for Minimap's Revealed tiles. "
"Acceptable range is [0 <= Explored <= Known <= Hidden <= 255].\n",
revealed);
return 0;
}
const int unseen = LuaToNumber(l, 3);
if (unseen < revealed || unseen > 255) {
PrintFunction();
ErrorPrint("Invalid value (%d) of opacity for Minimap's Unseen tiles. "
"Acceptable range is [0 <= Explored <= Known <= Hidden <= 255].\n",
unseen);
return 1;
LuaError(l,
"Invalid value (%d) of opacity for Minimap's Unseen tiles. "
"Acceptable range is [0 <= Explored <= Known <= Hidden <= 255].\n",
unseen);
return 0;
}

UI.Minimap.SetFogOpacityLevels(explored, revealed, unseen);
Expand Down Expand Up @@ -893,7 +899,7 @@ static int CclLoadTileModels(lua_State *l)
Map.TileModelsFileName = LuaToString(l, 1);
const fs::path filename = LibraryFileName(Map.TileModelsFileName.string());
if (LuaLoadFile(filename) == -1) {
ErrorPrint("Load failed: \"%s\"\n", filename.u8string().c_str());
LuaError(l, "Load failed: \"%s\"\n", filename.u8string().c_str());
}
return 0;
}
Expand Down
6 changes: 2 additions & 4 deletions src/missile/script_missile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,7 @@ void MissileType::Load(lua_State *l)
} else if (value == "ChangeVariable") {
const int index = UnitTypeVar.VariableNameLookup[LuaToString(l, -1)];// User variables
if (index == -1) {
ErrorPrint("Bad variable name '%s'\n", LuaToString(l, -1).data());
Exit(1);
return;
LuaError(l, "Bad variable name '%s'\n", LuaToString(l, -1).data());
}
this->ChangeVariable = index;
} else if (value == "ChangeAmount") {
Expand Down Expand Up @@ -244,7 +242,7 @@ static int CclMissile(lua_State *l)
PixelPos source(-1, -1);
Missile *missile = nullptr;

DebugPrint("FIXME: not finished\n");
LuaDebugPrint(l, "FIXME: not finished\n");

const int args = lua_gettop(l);
for (int j = 0; j < args; ++j) {
Expand Down
8 changes: 4 additions & 4 deletions src/pathfinder/script_pathfinder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ static int CclAStar(lua_State *l)
++j;
i = LuaToNumber(l, j + 1);
if (i <= 3) {
ErrorPrint("Fixed unit crossing cost must be strictly > 3\n");
LuaError(l, "Fixed unit crossing cost must be strictly > 3\n");
} else {
AStarFixedUnitCrossingCost = i;
}
} else if (value == "moving-unit-cost") {
++j;
i = LuaToNumber(l, j + 1);
if (i <= 3) {
ErrorPrint("Moving unit crossing cost must be strictly > 3\n");
LuaError(l, "Moving unit crossing cost must be strictly > 3\n");
} else {
AStarMovingUnitCrossingCost = i;
}
Expand All @@ -85,15 +85,15 @@ static int CclAStar(lua_State *l)
++j;
i = LuaToNumber(l, j + 1);
if (i < 0) {
ErrorPrint("Unseen Terrain Cost must be non-negative\n");
LuaError(l, "Unseen Terrain Cost must be non-negative\n");
} else {
AStarUnknownTerrainCost = i;
}
} else if (value == "max-search-iterations") {
++j;
i = LuaToNumber(l, j + 1);
if (i <= 0) {
ErrorPrint("Max A* search iterations must be strictly > 0\n");
LuaError(l, "Max A* search iterations must be strictly > 0\n");
} else {
AStarMaxSearchIterations = i;
}
Expand Down
5 changes: 2 additions & 3 deletions src/spell/script_spell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,7 @@ static void CclSpellAutocast(lua_State *l, AutoCastInfo *autocast)
if (var == "Distance") {
index = ACP_DISTANCE;
} else {
ErrorPrint("Bad variable name '%s'\n", var.data());
Exit(1);
LuaError(l, "Bad variable name '%s'\n", var.data());
}
}
autocast->PriorytyVar = index;
Expand Down Expand Up @@ -340,7 +339,7 @@ static int CclDefineSpell(lua_State *l)
SpellType *spell = nullptr;
if (it != SpellTypeTable.end()) {
spell = it->get();
DebugPrint("Redefining spell-type '%s'\n", identname.data());
LuaDebugPrint(l, "Redefining spell-type '%s'\n", identname.data());
} else {
SpellTypeTable.push_back(std::make_unique<SpellType>(SpellTypeTable.size(), std::string{identname}));
spell = SpellTypeTable.back().get();
Expand Down
Loading

0 comments on commit 879c6cf

Please sign in to comment.