Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6767949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:00:45+00:00 2026-05-26T15:00:45+00:00

I would like to embed some Lua script in my C application…..specifically for LOGGING.

  • 0

I would like to embed some Lua script in my C application…..specifically for LOGGING. I have used LOG4C before but am actually preferring “Lua Logging”….

NOTE: I am no Lua expert at all but am trying my best to get there soon 😉 Please bear with me…

Now I would like to know what the best way to do this is…meaning I want to LOG many events and process roughly 15 – 20 messages per seconds and each may have say 40 log messages. So if I call a C function for each message to be logged and then for EACH function call I do a “luaL_newstate”, “luaL_openlibs” and “lua_close” is that bad? It would appear to be but Lua is so lightweight and FAST I am wondering if it is that bad?

Obvioulsly I should rather have a “constructor” that is called ONCE for “luaL_newstate”, “luaL_openlibs” and then some sort of “destructor” for “”lua_close”..

lua_State *L;
L = luaL_newstate();                       
luaL_openlibs(L);       

...
...
if (luaL_loadfile(L, "test_log.lua")) 
    bail(L, "luaL_loadfile() failed");  

if (lua_pcall(L, 0, 0, 0))                
    bail(L, "lua_pcall() failed");       

lua_getglobal(L, "writeLog");        
lua_pushstring(L, buffer);
if (lua_pcall(L, 1, 1, 0))                  
    bail(L, "lua_pcall() failed"); 

int result = lua_tonumber(L, -1);
....
lua_close(L); 

Can you please advise me what the best solution would be here? Meaning if you say I must use a “Lua constructor”, can you please show me a small snippet of code to show me how this “logging process” should work in the best case scenario?

Thanks for the help 😉

Lynton

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:00:46+00:00Added an answer on May 26, 2026 at 3:00 pm

    Your question really has little to do with Lua. It’s really about how to put together a global system in C that needs to be initialized at some point, and then shut down when the program exits.

    I’m not a C expert, but the way I would do it is to have a pair of files, log_system.h and log_system.c. The log_system.h would have a function to log messages, a function to initialize the system, and a function to deinitialize it. The user of the logging system would then be required to initialize the system at the beginning of the program and deinitialize it at the end.

    The initialize function would set up a lua_State and store it in a static global variable. The deinitialize function would close the static lua_State. The logging function would check the lua_State and if it is valid (ie: non-NULL. The system has been initialized and not deinitialized), it will do whatever logging via Lua is necessary.

    There are other alternatives, like having a full-fledged object returned by the initialize function, which represents an instance of the logging system. This object would be an opaque pointer (for data hiding purposes) to an internal struct that contains a lua_State (or it could just return a void* that happens to be a lua_State, but I’d prefer the extensibility of an opaque struct).

    In this case, the user would be responsible for keeping that object wherever it needs to in order to do appropriate logging.

    Oh, and BTW, you have introduced a massive security hole in your application. The test_log.lua can be replaced by users to their heart’s content. And since you open all of the Lua libraries in that Lua state, they are free to write files to wherever they want.

    I would suggest doing some reasonable encapsulation here. Get rid of all functions Lua functions that touch files (dofile, loadfile, require, etc). Either that, or test_log.lua should not be an external file; it should be something built into your executable. This could be as an internal executable resource or just as a static global char* somewhere

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that I would like to embed inside our companies CMS.
I would like to embed Flash Player directly inside a C++ or Java application.
I'm working on a small GreaseMonkey script where I would like to embed a
Hey guys, I have developed a small site that i would like to embed
I have a ViewController that has its own NIB. I would like to embed
I would like to embed some C# .Net code that does some simple Encryption/Decryption
I have a lot of videos I would like to embed into an app,
I have a VB Express 2008 class library and would like to embed a
We presently have one big swf in flex 4. I would like to embed
I have some Python Scripts which I would like to use from my VB.NET

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.