<commit_before>﻿using CK.ControlChannel.Tcp;
using CK.Core;
using CK.Monitoring;
using System;
using System.IO;
using System.Text;

namespace CK.Glouton.Handler.Tcp
{
    public class TcpHandler : IGrandOutputHandler
    {
        private readonly TcpHandlerConfiguration _configuration;

        private ControlChannelClient _controlChannelClient;

        public TcpHandler( TcpHandlerConfiguration configuration )
        {
            _configuration = configuration ?? throw new ArgumentNullException( nameof( configuration ) );
        }

        public bool Activate( IActivityMonitor activityMonitor )
        {
            if( _controlChannelClient == null )
                _controlChannelClient = new ControlChannelClient
                (
                    _configuration.Host,
                    _configuration.Port,
                    _configuration.BuildAuthData(),
                    _configuration.IsSecure,
                    _configuration.RemoteCertificateValidationCallback,
                    _configuration.LocalCertificateSelectionCallback,
                    _configuration.ConnectionRetryDelayMs
                );

            if( _configuration.HandleSystemActivityMonitorErrors )
                SystemActivityMonitor.OnError += SystemActivityMonitor_OnError;

            _controlChannelClient.OpenAsync( activityMonitor ).GetAwaiter().GetResult();

            return true;
        }

        private void SystemActivityMonitor_OnError( object sender, SystemActivityMonitor.LowLevelErrorEventArgs @event )
        {
            _controlChannelClient.SendAsync( "SystemActivityMonitor.Error", GenerateLowLevelErrorPayload( @event ) );
        }

        private static byte[] GenerateLowLevelErrorPayload( SystemActivityMonitor.LowLevelErrorEventArgs @event )
        {
            using( var memoryStream = new MemoryStream() )
            using( var binaryWriter = new CKBinaryWriter( memoryStream, Encoding.UTF8, true ) )
            {
                binaryWriter.Write( @event.ErrorMessage );
                return memoryStream.ToArray();
            }
        }

        public bool ApplyConfiguration( IActivityMonitor activityMonitor, IHandlerConfiguration configuration )
        {
            return false;
        }

        public void Deactivate( IActivityMonitor m )
        {
            if( _controlChannelClient == null )
                return;

            _controlChannelClient.Dispose();
            _controlChannelClient = null;
        }

        public void Handle( GrandOutputEventInfo logEvent )
        {
            using( var memoryStream = new MemoryStream() )
            using( var binaryWriter = new CKBinaryWriter( memoryStream, Encoding.UTF8, true ) )
            {
                logEvent.Entry.WriteLogEntry( binaryWriter );
                _controlChannelClient.SendAsync( "GrandOutputEventInfo", memoryStream.ToArray() ).GetAwaiter().GetResult();
            }
        }

        public void OnTimer( TimeSpan timerSpan )
        {
        }
    }
}<commit_msg>We don't need to log errors, it's already done.<commit_after>﻿using CK.ControlChannel.Tcp;
using CK.Core;
using CK.Monitoring;
using System;
using System.IO;
using System.Text;

namespace CK.Glouton.Handler.Tcp
{
    public class TcpHandler : IGrandOutputHandler
    {
        private readonly TcpHandlerConfiguration _configuration;

        private ControlChannelClient _controlChannelClient;

        public TcpHandler( TcpHandlerConfiguration configuration )
        {
            _configuration = configuration ?? throw new ArgumentNullException( nameof( configuration ) );
        }

        public bool Activate( IActivityMonitor activityMonitor )
        {
            if( _controlChannelClient == null )
                _controlChannelClient = new ControlChannelClient
                (
                    _configuration.Host,
                    _configuration.Port,
                    _configuration.BuildAuthData(),
                    _configuration.IsSecure,
                    _configuration.RemoteCertificateValidationCallback,
                    _configuration.LocalCertificateSelectionCallback,
                    _configuration.ConnectionRetryDelayMs
                );

            _controlChannelClient.OpenAsync( activityMonitor ).GetAwaiter().GetResult();

            return true;
        }

        public bool ApplyConfiguration( IActivityMonitor activityMonitor, IHandlerConfiguration configuration )
        {
            return false;
        }

        public void Deactivate( IActivityMonitor m )
        {
            if( _controlChannelClient == null )
                return;

            _controlChannelClient.Dispose();
            _controlChannelClient = null;
        }

        public void OnTimer( IActivityMonitor m, TimeSpan timerSpan )
        {
            throw new NotImplementedException();
        }

        public void Handle( IActivityMonitor m, GrandOutputEventInfo logEvent )
        {
            using( var memoryStream = new MemoryStream() )
            using( var binaryWriter = new CKBinaryWriter( memoryStream, Encoding.UTF8, true ) )
            {
                logEvent.Entry.WriteLogEntry( binaryWriter );
                _controlChannelClient.SendAsync( "GrandOutputEventInfo", memoryStream.ToArray() ).GetAwaiter().GetResult();
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.06500279381635314, 'fraction_numerical': 0.0005587632706276774, 'mean_word_length': 2.0720823798627004, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '355691', 'n_tokens_mistral': 1248, 'n_tokens_neox': 1179, 'n_words': 271}