ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
I'm writing some system log software for Linux for a project and I am almost done but there is one piece that I am missing. The software needs to write the application messages to the log file and depending on user requirements (max log file size, number of messages, etc) remove the oldest message from the log. So I'm using a circular buffer to hold the messages. The question is when should I write to the log file? I cannot write every time data is modified in the buffer, that would be too taxing on the system. I am aware of the syslog software and was looking through the source to see when it writes to file, but was not able to find what I was looking for. Any ideas?
I know this isn't the answer you are looking for, but why don't you just use syslog?
As far as taxing the system goes, what language are you writing this in? Writing isn't necessarily done synchronously - for this reason some languages have commands like flush() and close() which force writing to be done.
Good question. The company I'm working for wants some features that syslog has. To include these features I would have needed to make a front end application to intercept messages. At that point I was basically writing the entire application anyhow, so we decided that it'd be best just to leave the busybox version currently running on the system there and utilize our own.
Thanks for the article Paul, while informative, it wasn't quite what I'm looking for. I'll try to be a little clearer. What I need is an algorithm for when to write to a file in a system log setting. When writing to disk is not possible every time data changes, what is the best approach?
Before you make the assumption that 'I cannot write every time data is modified in the buffer, that would be too taxing on the system', why don't you simply do it that way and see if your assumption is correct. You may be surprised to find how little load this adds. The filesystem is generally able to use algorithms employing buffering that does a good job of optimizing disk access efficiency.
Another factor that may make a difference is the type of filesystem used. Most have some particular emphasis on performance, and using the one that matches your requirements may be all the fine-tuning that your purpose requires.
Before you make the assumption that '...', why don't you simply do it that way and see if your assumption is correct.
The purpose of this system log is to monitor applications that are running on this board. We have a lot of them and they could all be spitting out a lot of messages at once. Writing every time a message is received is just not an option.
I guess what I'll do is create a series of flags (log file hasn't been written to in x minutes, buffer has x bytes of data more, etc) that will trigger a file write. It was my initial idea and seems very primitive, but I don't see any other solutions out there.