pcdshub / lcls-twincat-general

A PLC code toolkit for LCLS TwinCAT PLC projects
https://pcdshub.github.io/lcls-twincat-general/
Other
16 stars 20 forks source link

ENH: log to built-in twincat event logger as well as syslog + EPICS PV #13

Closed klauer closed 5 years ago

klauer commented 5 years ago

Closes #10

First pass at logging to the event logger + EPICS PV. To get the PV (:LogMessage), it will require the user to instantiate and pragma FB_Logger.

With the following code:

fbLogger(i_sMsg:='the message', i_eSevr:=E_MESGSEVR.Critical,
 i_eSubsystem:= E_Subsystem.MOTION);

The event logger outputs:

image

This message also goes via UDP to syslog -> logstash -> elastic search. The logstash JSON is:

``` { "_index": "logstash-2019.10.02-000001", "_type": "_doc", "_id": "d4W6E24B5y_5dq3dLbKU", "_version": 1, "_score": null, "_source": { "@timestamp": "2019-10-28T18:56:38.701Z", "syslog5424_app": "MOTION", "syslog5424_ts": "2019-10-28T19:39:01.099Z", "host": "gateway", "@version": "1", "port": 40080, "received_at": "2019-10-28T18:56:38.701Z", "message": "<162>1 2019-10-28T19:39:01.099Z plc-tst-proto2 MOTION VirtualMotor.Project.Main.fbLogger - - the message", "syslog5424_ver": "1", "syslog5424_proc": "VirtualMotor.Project.Main.fbLogger", "type": "syslog5424", "syslog5424_msg": "the message", "syslog5424_host": "plc-tst-proto2", "received_from": "gateway", "syslog5424_pri": "162" }, "fields": { "@timestamp": [ "2019-10-28T18:56:38.701Z" ] }, "sort": [ 1572288998701 ] } ```

This required adding a new EventType "LCLSLoggingEventClass" which maps error codes onto specific, fixed strings which are stored in the type system (such that the locale can be used to determine if it should be displayed in English or German). This data type is pinned such that it's accessible to projects which import the library:

image

I am not using this event logger system as it's intended, as you can see in the following: Error 163 10/28/2019 11:35:14 AM 920 ms | 'VirtualMotor.Project.Main.fbLogger: the message': Critical The event type gives the message "Critical" here, which should be the full user-defined message. The intention is to keep these fixed/unmodifiable at runtime.

However, I modify the "name" portion, in single quotes above ('VirtualMotor.Project.Main.fbLogger: the message') at runtime, repurposing it to hold our sPath and sMessage.

Does this seem a reasonable approach? (@ghalym @n-wbrown @slacAWallace @ZLLentz)

ZLLentz commented 5 years ago

To clarify: the approach is to modify the "name" field instead of the "message" because twincat events require pre-defined messages?

klauer commented 5 years ago

Yes @ZLLentz, as far as I know, that is a TwinCAT requirement. I'd be happy to be proven wrong, though.

ZLLentz commented 5 years ago

Seems like a strange choice (in the design of twincat). I suppose they decided that every library should define its own events? Either way this seems good, has the nice effect of syncing the twincat event feed and the syslog, and frees us from the tyranny of the event system tsars.

klauer commented 5 years ago

I believe it's like that for 2 main reasons: decoupling the messages from the code by requiring the definition of the events, and for ease of localization (DE/EN).

klauer commented 5 years ago

And I was proven wrong! Thanks @ghalym 😄

With ipArguments it's possible to tweak the message itself. Also, this PR doesn't touch on the listener which should've been included - i.e., for getting NC errors and such piped into syslog somehow.

I'll close this for now and open another with a better/more shiny solution.

ZLLentz commented 5 years ago

I was writing a long comment but now it's invalid. Looking forward to the next iteration.