Import SYSLOG - Pseudotype bug/error?

Linux specific forum
User avatar
makke
New User
New User
Posts: 6
Joined: Sun Nov 27, 2016 9:23 pm
Location: Germany
Contact:

Import SYSLOG - Pseudotype bug/error?

Post by makke »

Hello,

I Import the GNU C syslog functions to post in the system's syslog (I add an include at the end of the post). The import have some strange behavior, the syslog functions need the strings in ASCII format, so I use the pseudotype during import. But, the function openlog() didn't accept it, if I use the pseudotype, there are undefiened symbols in the syslog, e. g.:

Code: Select all

    Nov 25 12:29:32 xxxxxx-Linux ˆÍŽ[4649]: 4649]:
    Nov 25 12:33:13 xxxxxx-Linux Ḟŵâ[4687]:
    Nov 25 12:34:40 xxxxxx-Linux ˆ+Q"ú[4708]:
    Nov 25 12:35:14 xxxxxx-Linux Ḟ$Ö[4729]: 9]:
On the other way, the function syslog() accept the pseudotype. Anyone an idea whats wrong here ?

The include:

Code: Select all

    ; C header for syslog (GNU C utils)

    ; option flags for openlog
    #LOG_PID    = $01    ;log the pid with each message
    #LOG_CONS   = $02    ;log on the console if errors in sending
    #LOG_ODELAY = $04    ;delay open until first syslog() (Default)
    #LOG_NDELAY = $08    ;don't delay open
    #LOG_NOWAIT = $10    ;don't wait for console forks: DEPRECATED
    #LOG_PERROR = $20    ;log to stderr as well

    ; facility codes, I comment the not useful codes
    ;#LOG_KERN       = (0<<3)    ; kernel messages
    #LOG_USER       = (1<<3)    ; random user-level messages
    #LOG_MAIL       = (2<<3)    ; mail system
    #LOG_DAEMON     = (3<<3)    ; system daemons
    ;#LOG_AUTH       = (4<<3)    ; security/authorization messages
    ;#LOG_SYSLOG     = (5<<3)    ; messages generated internally by syslogd
    #LOG_LPR        = (6<<3)    ; line printer subsystem
    #LOG_NEWS       = (7<<3)    ; network news subsystem
    ;#LOG_UUCP       = (8<<3)    ; UUCP subsystem
    ;#LOG_CRON       = (9<<3)    ; clock daemon
    #LOG_AUTHPRIV   = (10<<3)   ; security/authorization messages (private)

    #LOG_NFACILITIES  = 24      ; current number of facilities
    #LOG_FACMASK      = $03f8   ; mask to extract facility part

    ; priorities (these are ordered)
    Enumeration 0
      #LOG_EMERG      ; system is unusable
      #LOG_ALERT      ; action must be taken immediately
      #LOG_CRIT       ; critical conditions
      #LOG_ERR        ; error conditions
      #LOG_WARNING    ; warning conditions
      #LOG_NOTICE     ; normal but significant condition
      #LOG_INFO       ; informational
      #LOG_DEBUG      ; debug-level messages
    EndEnumeration

    ; macro to create the facility_priority code for the SYSLOG command
    Macro LOG_MAKEPRI(fac, pri)
      (((fac) << 3) | (pri))
    EndMacro

    ; to get the syslog comamnds work inside purebasic, u have to import them
    ; this C functions need the strings in ASCII format
    ImportC ""
      openlog_ (*ident, option.i, facility.i = #Null) As "openlog"    ; I did not use the pseudotype, because it did not work at all, in the sysslog are undefined symbols
      syslog_  (facility_priority.i, format.p-ascii) As "syslog"
      closelog_() As "closelog"
    EndImport


    Procedure.i toAsc(String.s)
      Protected.l l = Len(String)
      Static.s ascString
      ascString = Space(l)
      PokeS(@ascString, String, l+1, #PB_Ascii)
      ProcedureReturn @ascString
    EndProcedure

    Macro SysLog(ProgramName, Facility, Priority, Message)
      ;openlog_(toAsc(ProgramName), #LOG_PID, Facility)  ; use the PID in the log and writes to stderr
      openlog_(toAsc(ProgramName), #LOG_PID)
      syslog_(LOG_MAKEPRI(Facility, Priority), Message)
      closelog_()
    EndMacro

    ; special internal constant, u can switch inside PB with DEFINED compiler function to enable syslog feature
    #ENABLE_SYSLOG = #True
---
Xubuntu 16.04 LTS (64 bit)
Debian 8 (64 bit)
Windows 7 (64 bit)

and, I apologize for my bad english.
User avatar
mariosk8s
Enthusiast
Enthusiast
Posts: 103
Joined: Wed Apr 06, 2011 11:37 am
Location: Hüfingen, Germany
Contact:

Re: Import SYSLOG - Pseudotype bug/error?

Post by mariosk8s »

I know it's a bit late, but i found and used your code. Setting ident does do weird stuff so i just ignored it. Here's my take on this.

Code: Select all

; C header for syslog (GNU C utils)
; option flags for openlog
#Syslog_PID    = $01    ;log the pid with each message
#Syslog_CONS   = $02    ;log on the console if errors in sending
#Syslog_ODELAY = $04    ;delay open until first syslog() (Default)
#Syslog_NDELAY = $08    ;don't delay open
#Syslog_NOWAIT = $10    ;don't wait for console forks: DEPRECATED
#Syslog_PERROR = $20    ;log to stderr as well

; facility codes
#Syslog_KERN       = (0<<3)    ; kernel messages
#Syslog_USER       = (1<<3)    ; random user-level messages
#Syslog_MAIL       = (2<<3)    ; mail system
#Syslog_DAEMON     = (3<<3)    ; system daemons
#Syslog_AUTH       = (4<<3)    ; security/authorization messages
#Syslog_SYSLOG     = (5<<3)    ; messages generated internally by syslogd
#Syslog_LPR        = (6<<3)    ; line printer subsystem
#Syslog_NEWS       = (7<<3)    ; network news subsystem
#Syslog_UUCP       = (8<<3)    ; UUCP subsystem
#Syslog_CRON       = (9<<3)    ; clock daemon
#Syslog_AUTHPRIV   = (10<<3)   ; security/authorization messages (private)

#Syslog_NFACILITIES  = 24      ; current number of facilities
#Syslog_FACMASK      = $03f8   ; mask to extract facility part

; priorities (these are ordered)
Enumeration 0
  #Syslog_EMERG      ; system is unusable
  #Syslog_ALERT      ; action must be taken immediately
  #Syslog_CRIT       ; critical conditions
  #Syslog_ERR        ; error conditions
  #Syslog_WARNING    ; warning conditions
  #Syslog_NOTICE     ; normal but significant condition
  #Syslog_INFO       ; informational
  #Syslog_DEBUG      ; debug-level messages
EndEnumeration

ImportC ""
  openlog(*ident, option.l, facility.l)
  syslog(priority.l, format.p-ascii, msg.p-utf8, *term = #Null)
  closelog()
EndImport

Procedure SyslogLog(priority.l, msg.s)
  openlog(0, #Syslog_PID, #Syslog_USER)
  syslog(priority, "%s", msg, 0)
  closelog()
EndProcedure

CompilerIf #PB_Compiler_IsMainFile
  SyslogLog(#Syslog_CRIT, "i log this")
  SyslogLog(#Syslog_WARNING, "i log that")
  SyslogLog(#Syslog_INFO, "i log for the heck of it")
  SyslogLog(#Syslog_NOTICE, "i log where i go")
  SyslogLog(#Syslog_DEBUG, "i log a lot")
CompilerEndIf

Running that produces the following in

Code: Select all

*** /var/log/syslog ***
Sep 24 11:45:51 kdev purebasic_compilation0.out[4450]: i log this
Sep 24 11:45:51 kdev purebasic_compilation0.out[4450]: i log that
Sep 24 11:45:51 kdev purebasic_compilation0.out[4450]: i log for the heck of it
Sep 24 11:45:51 kdev purebasic_compilation0.out[4450]: i log where i go
Sep 24 11:45:51 kdev purebasic_compilation0.out[4450]: i log a lot
Post Reply