All of lore.kernel.org
 help / color / mirror / Atom feed
From: oliver.schlenker@domain.hid
To: gilles.chanteperdrix@xenomai.org
Cc: jan.kiszka@domain.hid, xenomai@xenomai.org
Subject: Re: [Xenomai-help] :: rt_printf with daemonized task
Date: Fri, 9 Oct 2009 13:54:46 +0000	[thread overview]
Message-ID: <0000AD01.4ACF5CC5@domain.hid> (raw)






-------- Original Message --------
Subject: Re: :: rt_printf with daemonized task (09-Okt-2009 15:49)
From:    Gilles Chanteperdrix <gilles.chanteperdrix@xenomai.org>
To:      oliver.schlenker@smmotioncontrol.de

> oliver.schlenker@smmotioncontrol.de wrote:
> > +#define RT_PRINT_SYSLOG_STREAM	((FILE *)-9999)
> 
> -9999 may be a valid pointer. Only values between -4096 and 4095 can not
> be valid pointers.
> 
> -- 
>                                           Gilles
> 

ok, did change it to -99.



Oliver



--- rt_print.c.original	2008-09-10 10:36:27.000000000 +0200
+++ rt_print.c	2009-10-09 15:52:07.404908000 +0200
                 
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <syslog.h>
 
 #include <rtdk.h>
 #include <asm/xenomai/system.h>
                  
 
 #define RT_PRINT_LINE_BREAK		256
 
+#define RT_PRINT_SYSLOG_STREAM	((FILE *)-99)
+
 struct entry_head {
 	FILE *dest;
 	uint32_t seq_no;
+	int priority;
 	char text[1];
 } __attribute__((packed));
 
                   
 
 static void cleanup_buffer(struct print_buffer *buffer);
 static void print_buffers(void);
+static void rt_print_reinit_atfork_prepare(void);
+static void rt_print_reinit_atfork_child(void);
 
 /* *** rt_print API *** */
 
-int rt_vfprintf(FILE *stream, const char *format, va_list args)
+static int rt_print_to_buffer(FILE *stream, int priority, const char *format, va_list args)
 {
 	struct print_buffer *buffer = pthread_getspecific(__buffer_key);
 	off_t write_pos, read_pos;
                   
 			/* Write out empty entry */
 			head = buffer->ring + write_pos;
 			head->seq_no = __seq_no;
+			head->priority = 0;
 			head->text[0] = 0;
 
 			/* Forward to the ring buffer start */
                   
 	/* If we were able to write some text, finalise the entry */
 	if (len > 0) {
 		head->seq_no = ++__seq_no;
+		head->priority = priority;
 		head->dest = stream;
 
 		/* Move forward by text and head length */
                   
 		/* An empty entry marks the wrap-around */
 		head = buffer->ring + write_pos;
 		head->seq_no = __seq_no;
+		head->priority = priority;
 		head->text[0] = 0;
 
 		write_pos = 0;
                    
 	return res;
 }
 
+int rt_vfprintf(FILE *stream, const char *format, va_list args)
+{
+	return rt_print_to_buffer( stream, 0, format, args);
+}
+
 int rt_vprintf(const char *format, va_list args)
 {
 	return rt_vfprintf(stdout, format, args);
                    
 	return n;
 }
 
+void rt_syslog(int priority, char *format, ...)
+{
+	va_list args;
+
+	va_start(args, format);
+	rt_print_to_buffer(RT_PRINT_SYSLOG_STREAM, priority, format, args);
+	va_end(args);
+
+	return;
+}
+
+void rt_vsyslog(int priority, char *format, va_list args )
+{
+
+	rt_print_to_buffer(RT_PRINT_SYSLOG_STREAM, priority, format, args);
+
+	return;
+}
+
 static void set_buffer_name(struct print_buffer *buffer, const char *name)
 {
 	int n;
                    
 
 		if (len) {
 			/* Print out non-empty entry and proceed */
-			fprintf(head->dest, "%s", head->text);
+			/* Check if output goes to syslog */
+			if (head->dest == RT_PRINT_SYSLOG_STREAM) {
+			   	syslog( head->priority, "%s", head->text );
+			} else {
+				/* Output goes to specified stream */
+				fprintf(head->dest, "%s", head->text);
+			}
+
 			read_pos += sizeof(*head) + len;
 		} else {
 			/* Emptry entries mark the wrap-around */
                    
 	}
 }
 
+static void rt_print_reinit_atfork_child(void)
+{
+	pthread_attr_t thattr;
+	struct print_buffer *buffer,*next_b;
+
+	/* Clean parent process buffer list -
+	   Shouldn't be necessary because also done at atfork_parent
+	*/
+	buffer = __first_buffer;
+	while (buffer) {
+		next_b = buffer->next;
+		cleanup_buffer(buffer);
+		buffer = next_b;
+	}
+
+	/* Throw new thread with printer loop in child process */
+	pthread_attr_init(&thattr);
+	pthread_attr_setstacksize(&thattr, PTHREAD_STACK_MIN);
+	pthread_create(&__printer_thread, &thattr, printer_loop, NULL);
+	pthread_atfork(rt_print_reinit_atfork_prepare,NULL,rt_print_reinit_atfork_child);
+}
+
+static void rt_print_reinit_atfork_prepare(void)
+{
+	struct print_buffer *buffer,*next_b;
+
+	/* Clean parent process buffer list */
+	buffer = __first_buffer;
+	while (buffer) {
+		next_b = buffer->next;
+		cleanup_buffer(buffer);
+		buffer = next_b;
+	}
+}
+
 void __rt_print_init(void)
 {
 	pthread_attr_t thattr;
                   
 	pthread_attr_init(&thattr);
 	pthread_attr_setstacksize(&thattr, PTHREAD_STACK_MIN);
 	pthread_create(&__printer_thread, &thattr, printer_loop, NULL);
+	pthread_atfork(rt_print_reinit_atfork_prepare,NULL,rt_print_reinit_atfork_child);
 } 

To: gilles.chanteperdrix@xenomai.org
Cc: jan.kiszka@siemens.com
    xenomai-help@gna.org

             reply	other threads:[~2009-10-09 13:54 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-10-09 13:54 oliver.schlenker [this message]
  -- strict thread matches above, loose matches on Subject: below --
2009-10-20 10:03 [Xenomai-help] :: rt_printf with daemonized task oliver.schlenker
2009-10-20 16:01 ` Gilles Chanteperdrix
2009-10-20 16:07   ` Jan Kiszka
2009-10-20 16:11 ` Jan Kiszka
2009-10-20  9:26 oliver.schlenker
2009-10-20  9:33 ` Jan Kiszka
2009-10-19 13:14 oliver.schlenker
2009-10-19 14:00 ` Jan Kiszka
2009-10-19 13:12 oliver.schlenker
2009-10-19 14:00 ` Jan Kiszka
2009-10-09 14:48 oliver.schlenker
2009-10-09 14:59 ` Jan Kiszka
2009-10-09 13:34 oliver.schlenker
2009-10-09 13:49 ` Gilles Chanteperdrix
2009-10-09 13:58 ` Jan Kiszka
2009-10-08 12:01 oliver.schlenker
2009-10-08 13:17 ` Jan Kiszka
2009-10-08  7:15 [Xenomai-help] " oliver.schlenker
2009-10-06 15:10 oliver.schlenker
2009-10-06 15:16 ` Gilles Chanteperdrix

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=0000AD01.4ACF5CC5@domain.hid \
    --to=oliver.schlenker@domain.hid \
    --cc=gilles.chanteperdrix@xenomai.org \
    --cc=jan.kiszka@domain.hid \
    --cc=xenomai@xenomai.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.