Example #1
0
        /// <summary>
        /// Channel logger constructor.
        /// </summary>
        /// <param name="channelId">Unique logger id</param>
        /// <param name="stream">Logging stream. Should be writable.</param>
        /// <param name="buffer">Serialization buffer. Can be shared between multiple loggers (non thread safe).</param>
        /// <param name="tracer">Optional realtime message tracer. Useful in debug environment.</param>
        /// <exception cref="BinLogException">
        /// Thrown when size of <c>TChannelEnum</c> or <c>TMessageEnum</c> is invalid.
        /// </exception>
        protected Logger(TChannelEnum channelId, Stream stream, byte[] buffer, LogTracer tracer = null)
        {
            if (!LogEnum.CheckSizeOf <TChannelEnum>())
            {
                throw new BinLogException($"Size of {nameof(TChannelEnum)} should be {sizeof(ushort)}");
            }

            if (!LogEnum.CheckSizeOf <TMessageEnum>())
            {
                throw new BinLogException($"Size of {nameof(TMessageEnum)} should be {sizeof(ushort)}");
            }

            _stream = stream;
            _buffer = buffer;

            if (!_stream.CanWrite)
            {
                throw new BinLogException("Stream doesn't support writing");
            }

            _tracer = tracer;
            _name   = channelId.ToString();

            _channelId = LogEnum.ToUInt16(channelId);
        }
Example #2
0
        /// <summary>
        /// Channel decoder constructor.
        /// </summary>
        /// <param name="channel">Unique logger id</param>
        /// <param name="argDecoder">Binary argument deserializer</param>
        /// <exception cref="BinLogException">
        /// Thrown when size of <c>TChannelEnum</c> or <c>TMessageEnum</c> is invalid.
        /// </exception>
        public ChannelDecoder(TChannelEnum channel, ArgumentDecoder argDecoder)
        {
            if (!LogEnum.CheckSizeOf <TChannelEnum>())
            {
                throw new BinLogException($"Size of {nameof(TChannelEnum)} should be {sizeof(ushort)}");
            }

            if (!LogEnum.CheckSizeOf <TMessageEnum>())
            {
                throw new BinLogException($"Size of {nameof(TMessageEnum)} should be {sizeof(ushort)}");
            }

            _argDecoder = argDecoder;

            ChannelId   = LogEnum.ToUInt16(channel);
            ChannelName = channel.ToString();
        }