hacking and logging async server : test on ejabberd
Par Mathieu Lecarme le jeudi, 3 mars 2011, 23:32 - Lien permanent
Async server is marvelous for handling lots of parralels connection. Understanding what happens inside it is a challenge.
Ejabberd is a mature erlang server with a real world application : chatting.
Ejabberd provide a live console. Very helpful for crash during start (with your own module, provided one a pretty stable), irritating in use in real condition. All things happen in the same window. You wont to enter some commands, taddam, ugly logs hide what you are typing. Logs are multines, with 80 columns very useful on your 1980 Terminal and your stone keyboard.
I wont two things : using ejabberd on a distant Linux, working on a Unix computer. Client/server is needed.
Debugd
Here is debugd, a basic json broadcaster over TCP/IP. Launch the server (it's an OTP application), connect with a client, broadcast message. The protocol is simple, 4 bytes for the message size followed by a Json encoded message. Python example is provided, you can build your own easily.
Hacking Ejabberd
Ugly hack in ejabberd_app.erl to start start the application in the start/2 function :
application:start(debugd),
Polite person should make a module wich start the debugd application.
Cleaner hack in ejabberd_loglevel.erl for triggering messages. Ejabberd use a trick for logging, the code is made on the fly. I add my code inside after the "(Module, Line, Format, Args) ->
debugd:json({struct, [
{module, Module},
{line, Line},
{message, iolist_to_binary(io_lib:format(Format, Args))}
]}),
You have to be quick, now. Launch ejabberd with ejabberdctl and in an other winddow the client.py . Now, magic happens :
$ ./client.py
{u'line': 37, u'message': u'ejabberd has not been compiled with relational database support. Skipping database startup.', u'module': u'ejabberd_rdbms'}
{u'line': 166, u'message': u'Reusing listening port for 5222', u'module': u'ejabberd_listener'}
{u'line': 166, u'message': u'Reusing listening port for 5269', u'module': u'ejabberd_listener'}
{u'line': 166, u'message': u'Reusing listening port for 5280', u'module': u'ejabberd_listener'}
{u'line': 73, u'message': u'ejabberd 2.1.6 is started in the node ejabberd@localhost', u'module': u'ejabberd_app'}
{u'line': 73, u'message': u'ejabberd 2.1.6 is started in the node ejabberd@localhost', u'module': u'ejabberd_app'}
It works. But it's just a POC, I'd like to know the process ID and parents (where I am), and need some works on the display : filter, colors, info on the server status, crash report ...
Finding a way to start the debugger client when the server start can also be a good idea.
To be continued.

