-- Copyright (c) 2015 gutlib.com - All rights reserved. Terms are in gutlib.sql
-- You are NOT ALLOWED to modify and/or use this code without author permission

if object_id('g.log_seq') is null
   create sequence g.log_seq START WITH 1 INCREMENT BY 1 ;
go
if object_id('g.log') is null
   exec('create procedure g.log as
      RAISERROR (''g.log.err.1: not implemented'' ,16 ,1) WITH NOWAIT')
go
alter procedure g.log
    @arg1 nvarchar(max) = null
   ,@arg2 nvarchar(max) = null
   ,@arg3 nvarchar(max) = null
   ,@arg4 nvarchar(max) = null
   ,@arg5 nvarchar(max) = null
   ,@err int = null
as
if @err is null set @err = 0

declare @arg nvarchar(max) = ''
set @arg += isnull(      @arg1,'')
set @arg += isnull(' ' + @arg2,'')
set @arg += isnull(' ' + @arg3,'')
set @arg += isnull(' ' + @arg4,'')
set @arg += isnull(' ' + @arg5,'')

set @arg = ''
   + convert(varchar,NEXT VALUE FOR g.log_seq) + ')'
   + ' ' + convert(varchar,getdate(),114)
   + '  ' + isnull(replace(convert(varchar(128),context_info()),0x00,'') + '.' ,'')
   + @arg

--print @arg
-- using RAISERROR with 0 severity and NOWAIT option as trick to push messages to console immediately
RAISERROR (@arg, @err, 1) WITH NOWAIT
go
declare @cx varbinary(128) = convert(varbinary(128), 'g.log.test'); set context_info @cx
exec g.log 'line 1'
exec g.log line ,2
