<commit_before>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;

using Couchbase.Lite.DI;
using Couchbase.Lite.Interop;

using JetBrains.Annotations;

using LiteCore;
using LiteCore.Interop;
using LiteCore.Util;

namespace Couchbase.Lite.Logging
{
    public sealed class FileLogger : ILogger
    {
        #region Variables

        [NotNull]private string _directory;
        private int _maxRotateCount;
        private long _maxSize;
        private bool _usePlaintext;
        private bool _hasConfigChanges;

        [NotNull]
        private readonly Dictionary<LogDomain,IntPtr> _domainObjects = new Dictionary<LogDomain, IntPtr>();

        #endregion

        #region Properties

        public string Directory
        {
            get => _directory;
            set {
                SetAndReact(ref _directory, value ?? DefaultDirectory());
                System.IO.Directory.CreateDirectory(_directory);

                // This one should update immediately since it won't affect rotation
                UpdateConfig();
            } 
        }

        public int MaxRotateCount
        {
            get => _maxRotateCount;
            set => SetAndReact(ref _maxRotateCount, value);
        }

        public long MaxSize
        {
            get => _maxSize;
            set => SetAndReact(ref _maxSize, value);
        }

        public bool UsePlaintext
        {
            get => _usePlaintext;
            set => SetAndReact(ref _usePlaintext, value);
        }

        public LogLevel Level
        {
            get => (LogLevel)Native.c4log_binaryFileLevel();
            set => Native.c4log_setBinaryFileLevel((C4LogLevel) value);
        }

        #endregion

        #region Constructors

        public FileLogger()
        {
            _maxRotateCount = 1;
            _maxSize = 1024;
            SetupDomainObjects();
            Level = LogLevel.Info;
            _directory = DefaultDirectory();
        }

        private unsafe void SetupDomainObjects()
        {
            var bytes = (byte *)Marshal.StringToHGlobalAnsi("Couchbase");
            _domainObjects[LogDomain.Couchbase] = (IntPtr)Native.c4log_getDomain(bytes, true);

            bytes = (byte *)Marshal.StringToHGlobalAnsi("DB");
            _domainObjects[LogDomain.Database] = (IntPtr)Native.c4log_getDomain(bytes, true);

            bytes = (byte *)Marshal.StringToHGlobalAnsi("Query");
            _domainObjects[LogDomain.Query] = (IntPtr)Native.c4log_getDomain(bytes, true);

            bytes = (byte *)Marshal.StringToHGlobalAnsi("Sync");
            _domainObjects[LogDomain.Replicator] = (IntPtr)Native.c4log_getDomain(bytes, true);

            foreach (var domain in _domainObjects) {
                Native.c4log_setLevel((C4LogDomain *)domain.Value.ToPointer(),
                    C4LogLevel.Debug);
            }
        }

        #endregion
<commit_msg>Update default max size for file logger<commit_after>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;

using Couchbase.Lite.DI;
using Couchbase.Lite.Interop;

using JetBrains.Annotations;

using LiteCore;
using LiteCore.Interop;
using LiteCore.Util;

namespace Couchbase.Lite.Logging
{
    public sealed class FileLogger : ILogger
    {
        #region Variables

        [NotNull]private string _directory;
        private int _maxRotateCount;
        private long _maxSize;
        private bool _usePlaintext;
        private bool _hasConfigChanges;

        [NotNull]
        private readonly Dictionary<LogDomain,IntPtr> _domainObjects = new Dictionary<LogDomain, IntPtr>();

        #endregion

        #region Properties

        public string Directory
        {
            get => _directory;
            set {
                SetAndReact(ref _directory, value ?? DefaultDirectory());
                System.IO.Directory.CreateDirectory(_directory);

                // This one should update immediately since it won't affect rotation
                UpdateConfig();
            } 
        }

        public int MaxRotateCount
        {
            get => _maxRotateCount;
            set => SetAndReact(ref _maxRotateCount, value);
        }

        public long MaxSize
        {
            get => _maxSize;
            set => SetAndReact(ref _maxSize, value);
        }

        public bool UsePlaintext
        {
            get => _usePlaintext;
            set => SetAndReact(ref _usePlaintext, value);
        }

        public LogLevel Level
        {
            get => (LogLevel)Native.c4log_binaryFileLevel();
            set => Native.c4log_setBinaryFileLevel((C4LogLevel) value);
        }

        #endregion

        #region Constructors

        public FileLogger()
        {
            _maxRotateCount = 1;
            _maxSize = 1024 * 500;
            SetupDomainObjects();
            Level = LogLevel.Info;
            _directory = DefaultDirectory();
        }

        private unsafe void SetupDomainObjects()
        {
            var bytes = (byte *)Marshal.StringToHGlobalAnsi("Couchbase");
            _domainObjects[LogDomain.Couchbase] = (IntPtr)Native.c4log_getDomain(bytes, true);

            bytes = (byte *)Marshal.StringToHGlobalAnsi("DB");
            _domainObjects[LogDomain.Database] = (IntPtr)Native.c4log_getDomain(bytes, true);

            bytes = (byte *)Marshal.StringToHGlobalAnsi("Query");
            _domainObjects[LogDomain.Query] = (IntPtr)Native.c4log_getDomain(bytes, true);

            bytes = (byte *)Marshal.StringToHGlobalAnsi("Sync");
            _domainObjects[LogDomain.Replicator] = (IntPtr)Native.c4log_getDomain(bytes, true);

            foreach (var domain in _domainObjects) {
                Native.c4log_setLevel((C4LogDomain *)domain.Value.ToPointer(),
                    C4LogLevel.Debug);
            }
        }

        #endregion

{'fraction_non_alphanumeric': 0.08711096075778078, 'fraction_numerical': 0.005581867388362652, 'mean_word_length': 2.055813953488372, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 25, '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': '6952747', 'n_tokens_mistral': 1630, 'n_tokens_neox': 1580, 'n_words': 335}