logger_service.anubis 12.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
/*
 * 
 * User: David RENE
 * Date: 29/07/2007
 * Time: 02:04
 * (c) Calexium
 *
 */

read tools/basis.anubis
read system/muscle.anubis
read system/data_io.anubis
read system/convert.anubis
read system/string.anubis
read system/logger.anubis
read system/message_queue.anubis
read system/message_transceiver.anubis

read calexium_lib/CXM_message_constants.anubis
read calexium_lib/net_services/CXM_net_services.anubis
read calexium_lib/net_services/CXM_generic_protocol.anubis

// Special non-existing file. This is a work arround for a impossibility to define global configuration in anubis.
// So this file allows you to specify on which UDP port the logger server is listening.
// You have to provide this file in your base source directory (or any other folder which is scanned by the compiler,
// and you define a function named 'logger_port' which simply return the port number as an Word32.
// Example: 
//     public define Word32 logger_port = 33610.
read config/logger_config.anubis

// previous 'read' replaced by an empty declaration. Should be defined into main source file.
 public define Word32 logger_port.

public define Word32 logger_srv_version = 1.

  /** 
   *  has_update scan the given message for finding corresponding available update
   *  package for the application. The request message come with AppId field which contain
   *  package or application signature and CurrentVersion field which is current version
   *  installed on the requester.
   * 
   * --- REQUEST message format
   *  msg [_CXM_UPD_HAS_UPDATE]
   *      string "AppId" = hash or unique signature of the package
   *      string "CurrentVersion" = current version installed on the remote caller
   * 
   * --- ANSWER message format
   *  
   *  ERROR case message format 
   *  msg [_CXM_ACK]
   *      int32 "CMD" = _CXM_UPD_HAS_UPDATE
   *      int32 "STATUS" = _CXM_ERROR
   *  
   *  OK without update available
   *  msg [_CXM_ACK]
   *      int32 "CMD"    = _CXM_UPD_HAS_UPDATE
   *      int32 "STATUS" = _CXM_OK
   *      msg "RESULT" [_CXM_ACK_RESULT_MSG] 
   *          int32 "PackageStatus" = no_update // numerical value = 0
   * 
   */
   
define Bool 
  process_log
  (
    Logger    log,
    Message   msg
  )=
  if find_string(msg, "LogString") is
  {
    failure             then 
      //println("process_log can't find LogString");
      logError(log, "process_log can't find LogString");false,
      
    success(log_string) then
    if find_int32(msg, "Level") is
    {
      failure             then 
        //println("process_log can't find Level ["+log_string+"]");
        logError(log, "process_log can't find Level");false,
      success(int_level)  then
        if find_int32(msg, "Thread") is
        {
          failure             then 
            //println("process_log can't find Thread ["+log_string+"]");
            logError(log, "process_log can't find Thread");false,
          success(thread_id)  then
            //println("*-*-> t["+thread_id+"] l["+int_level+"] s["+log_string+"]");
            with level = get_LogLevel_from_value(to_Int(int_level)),
            if level is logTrace then
                if find_string(msg, "LogMask") is
                {
                  failure           then 
                    //println("process_log can't find LogMask ["+log_string+"]");
                    logError(log, "process_log can't find LogMask");false,
                  success(log_mask) then logTrace(log, logMask(log_mask), log_string, thread_id); true
                }
            else doLog(log, log_string, level, thread_id); true
//            {
//              logNone           then  true,
//              logCriticalError  then  logCriticalError(log, log_string); true,
//              logError          then  logError(log, log_string); true,
//              logWarning        then  logWarning(log, log_string); true,
//              logInfo           then  logInfo(log, log_string); true,
//              logDebug          then  logDebug(log, log_string); true
//              logTrace          then  
//                if find_string(msg, "LogMask") is
//                {
//                  failure           then logError(log, "process_log can't find LogMask");false,
//                  success(log_mask) then logTrace(log, logMask(log_mask),log_string); true
//                }
//            }
        }
    }
	}.

define Bool 
  process_log
  (
    List(Logger)    logs,
    Message         msg
  )=
  if find_string(msg, "LogName") is
  {
    failure         then println("process_log can't find LogName");false,
    success(log_name) then
    if get_logger(logs, log_name) is
    {
      failure       then println("can't find log ["+log_name+"]");false,
      success(log)  then process_log(log, msg)
    }
  }.
  
  /** message_received is the principal 
   */
 /*
 define One
  message_received
  (
    MessageQueue    queue,
    List(Logger)    logs,
    Message         msg
  )=
  with msg_what = *msg.what,
  if msg_what = _CXM_LOGGER_LOG then
    send_result(queue, _CXM_LOGGER_LOG, process_log(logs, msg))
  else
    println("wrong message_received is not _CXM_LOGGER_LOG ");unique
  .
  
 define One
  logger_handler
  (
    MessageQueue    queue,
    List(Logger)    logs
  )=
  if queue.get_next_received_Message(1) is
  {
    timeout   then  //println("logger_handler timeout ["+queue.get_name(unique)+"]");
      logger_handler(queue, logs),
    closed    then  //println("logger_handler closed ["+queue.get_name(unique)+"]");
      unique,
    msg(msg)  then  //println("logger_handler msg received ["+queue.get_name(unique)+"]");
      message_received(queue, logs, msg);
      logger_handler(queue, logs)
  }.

 public define NetService
  logger_service
  (
    List(Logger) logs
  )=
    net_service(
                logger_srv_version, 
                _CXM_LOGGER_SERVICE_ID, //id of the service declared in CXM_message_constants
                "Logger service",       //human readable name of the service
                (
                  MessageQueue queue,
                  String       peer
                ) |-> logger_handler(queue, logs)).

 */
 /****************** LOCAL UDP SERVICE *************/
 

public define String 
  data_preview
  (
    ByteArray data,
    Int       max
  ) =
  with l = length(data),
       firsts_bytes = extract(data, 0, max),
  "Length=" + l + "; String='"+to_string(firsts_bytes)+"' Buffer=[" + to_ascii(firsts_bytes) + (if l > max then "...]" else "]").

define (UDP_Socket, ByteArray, Truncation, Word32, Word32) -> One
  make_udp_handler
    (
       List(Logger) logs,
       Logger       debug_logger
    ) =
  (
    UDP_Socket socket,
    ByteArray  data,
    Truncation truncation, 
    Word32     ip_address,
    Word32     ip_port
  ) |->
  with ip = ip_addr_to_string(ip_address),
  logTrace(debug_logger, logMask("Logger"), "LoggerServer: data received (" + length(data) + " bytes) from IP " + ip + "... ");
  //println("LoggerServer: data received (" + length(data) + " bytes) from IP " + ip + "... ");
  //println("LoggerServer: data_preview " + data_preview(data, 20));

  if receive_message_from_io(make_data_io(data)) is
  {
    failure then 
      //println("LoggerServer: Bad data packet received from IP " + ip + ": " + data_preview(data, 20));
      logWarning(debug_logger, "LoggerServer: Bad data packet received from IP " + ip + ": " + data_preview(data, 20)),
    success(msg) then
      if *msg.what = _CXM_LOGGER_LOG then
        forget(process_log(logs, msg))
      else
        println("LoggerService: wrong message_received. [" + to_hexa(*msg.what) + "] is not _CXM_LOGGER_LOG ")
}.

define One
  my_notify
     (
       One dummy
     ) =
    unique.

public define Maybe(UDP_Server)
   start_logger_server
     (
       List(Logger) logs,
       Logger       debug_logger
     ) =
  if start_udp_server(0,
                      logger_port, 
                      make_udp_handler(logs, debug_logger),
                      8*1024,
                      my_notify) is
  {
    cannot_create_the_socket then logError(debug_logger, "Cannot create the listening socket."); failure, 
    cannot_bind_address_port then logError(debug_logger, "Cannot bind to port " + logger_port + "."); failure,
    access_denied            then logError(debug_logger, "Cannot listen on port " + logger_port + "."); failure,
    ok(server)               then logInfo(debug_logger, "LocalLog Server started on port " + logger_port + "."); success(server)
  }.
   

 /****************** CLIENT PART *******************/
 
define One
  local_net_logger
  (
    String  logger_server,
    Message log_message
  )=
  forget(send_message_by_udp(log_message, ip_address((127,0,0,1)), logger_port)).


define Message
  create_log_msg
  (
    String  logger_name,
    String  log_string,
    Int     level
  )=
  with len = length(log_string),
  //println("len log string "+len);
  with string = if length(log_string) > 2048 then (if sub_string(log_string, 0, 2048) is success(s) then s else "?") + "... original log string length "+len
                                             else log_string,
  with log_msg = message(_CXM_LOGGER_LOG),
  forget(add_string(log_msg, "LogName", logger_name));
  forget(add_string(log_msg, "LogString", string));
  forget(add_int32(log_msg, "Level", truncate_to_Word32(level)));
  forget(add_int32(log_msg, "Thread", virtual_machine_id));
  log_msg
  .
*/

// HACK Begin of Hacked version
  define String debug_log    = "Debug".
  define String config_log   = "Config".
  define String incoming_log = "Incoming".
  define String outgoing_log = "Outgoing".
  define String pop3_log     = "POP3".
  define String mailing_log  = "Mailing".
  define String errors_log   = "Errors".
  define String security_log = "Security".
  define String spams_log    = "Spams".
  define String api_log      = "MF_API".
  define String internal_log = "INTERNAL".
  
             
  define String 
    get_log_file
    (
      String log_name
    ) =
         if log_name = debug_log then "debug.log"
    else if log_name = "SendMail"     then "debug.log"
    else if log_name = config_log     then "config.log"
    else if log_name = incoming_log   then "incoming.log"
    else if log_name = outgoing_log   then "outgoing.log"
    else if log_name = pop3_log       then "pop3.log"
    else if log_name = mailing_log    then "mailing.log"
    else if log_name = errors_log     then "errors.log"
    else if log_name = "DB"           then "errors.log"
    else if log_name = "SQL"          then "sql_profiling.log"
    else if log_name = security_log   then "security.log"
    else if log_name = spams_log      then "spams.log"
    else if log_name = api_log        then "api.log"
    else if log_name = internal_log   then "internal"
    else "unknown.log".
    
             
  define One
    local_net_logger
    (
      String  logger_server,
      One     _
    )=
    unique.
  
  define One
    create_log_msg
    (
      String  logger_name,
      String  log_string,
      Int     level
    )=
    with log = createLogger(logger_name, 8, "/var/MailFountain/log/" + get_log_file(logger_name), logTrace, logTrace),
         logLevel = get_LogLevel_from_value(level),
    protect
    doLog(log, log_string, logLevel, virtual_machine_id).

// HACK End of Hacked version



public define One 
  log( 
    LogLevel  level,
    String    logger_name, 
    String    log_string
  )=
  local_net_logger("127.0.0.1", create_log_msg(logger_name, log_string, get_log_level_value(level))).

public define One 
  logNone( 
    String logger_name, 
    String log_string
  )=
  local_net_logger("127.0.0.1", create_log_msg(logger_name, log_string, 0)).

public define One 
  logCriticalError( 
    String logger_name, 
    String log_string
  )=
  local_net_logger("127.0.0.1", create_log_msg(logger_name, log_string, 1)).

public define One 
  logError( 
    String logger_name, 
    String log_string
  )=
  local_net_logger("127.0.0.1", create_log_msg(logger_name, log_string, 2)).

public define One 
  logWarning( 
    String logger_name, 
    String log_string
  )=
  local_net_logger("127.0.0.1", create_log_msg(logger_name, log_string, 3)).
  
public define One 
  logInfo( 
    String logger_name, 
    String log_string
  )=
  local_net_logger("127.0.0.1", create_log_msg(logger_name, log_string, 4)).

public define One 
  logDebug( 
    String logger_name, 
    String log_string
  )=
  local_net_logger("127.0.0.1", create_log_msg(logger_name, log_string, 5)).

public define One 
  logTrace( 
    String  logger_name,
    LogMask log_mask,
    String  log_string
  )=
  with log_msg = create_log_msg(logger_name, log_string, 6),
  forget(add_string(log_msg, "LogMask", log_mask.mask));
  local_net_logger("127.0.0.1", log_msg).

// HACK hacked version
 public define One 
  logTrace( 
    String  logger_name,
    LogMask log_mask,
    String  log_string
  )=
  if log_mask is logMask(mask) then
  if mask = "send_mail" | mask = "smtp" | mask = "sql_profiling"  then
    local_net_logger("127.0.0.1", create_log_msg(logger_name, log_string, 6))
  else unique.