39

Turning MagicalRecord logging off requires a #define to be made before it is first included in the project, but in the case of a project managed by Cocoapods I have no access to add a #define in the Pods project. How do I turn logging off completely in this scenario?

Took me a few hours to figure out a way to do it, posting here in the hope it will help others.

EDIT: this is not a duplicate as it discusses turning logging off under Cocoapods

Andres Kievsky
  • 3,461
  • 32
  • 25
  • Possible duplicate of [Disable MagicalRecord error messages and warnings](http://stackoverflow.com/questions/12908584/disable-magicalrecord-error-messages-and-warnings) – Alberto Scampini Jan 26 '16 at 09:59

5 Answers5

79

You can use a post_install hook to modify pretty much any build setting. Just add this code to your Podfile:

post_install do |installer|
  target = installer.project.targets.find{|t| t.to_s == "Pods-MagicalRecord"}
    target.build_configurations.each do |config|
        s = config.build_settings['GCC_PREPROCESSOR_DEFINITIONS']
        s = [ '$(inherited)' ] if s == nil;
        s.push('MR_ENABLE_ACTIVE_RECORD_LOGGING=0') if config.to_s == "Debug";
        config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] = s
    end
end

Note that this will only disable logging in the debug configuration - logging is disabled by default in the release configuration.

Andres Kievsky
  • 3,461
  • 32
  • 25
  • No worries! Would love to hear other approaches to this problem. – Andres Kievsky Mar 08 '13 at 02:42
  • 2
    Since I rarely run `pod update` I simply added it to my pods pch file. – Keith Smiley Mar 08 '13 at 13:42
  • 1
    Thanks for sharing! I'm not very familiar with ruby, but it seems you have a typo on the line if s==nil : s = [ '$(inherited)' ] end The : symbol is not necessary. I removed it and everything started working. – Den Telezhkin Jul 25 '13 at 08:04
  • THANKS!!! I was really getting crazy on this one! :) It's worth keeping an eye on [this issue](https://github.com/CocoaPods/CocoaPods/issues/833) on the CocoaPods repo, which seems to be discussing a better way to do this in the future – mokagio Sep 25 '13 at 12:00
  • 2
    Re: typo - you could just put `s ||= [ '$(inherited)' ]`. – Michael Forrest Oct 03 '13 at 11:13
  • 1
    However, this solution doesn't work for me - I'm on MagicalRecord 2.1. I can't seem to silence the logs even by hacking the pod's .pch file either. I'm on CocoaPods 0.25.0 – Michael Forrest Oct 03 '13 at 11:14
  • [!] Invalid `Podfile` file: Podfile:16: syntax error, unexpected ':', expecting keyword_then or ';' or '\n' if s==nil : s = [ '$(inherited)' ] end – pronebird Oct 18 '13 at 12:31
13

In my case, I was building a library that depended on MagicalRecord. I didn't want my users to have to add a post_install in their Podfile to silence the noisy logging, so I added it to my podspec instead.

  s.prefix_header_contents = '#define MR_ENABLE_ACTIVE_RECORD_LOGGING 0'

This automatically adds this #define statement to Pods-prefix.pch, which silences MagicalRecord logging in projects that use my pod.

Cody A. Ray
  • 5,869
  • 1
  • 37
  • 31
5

I updated ank's answer for those using the new cocoapods version alongside MagicalRecord 2.3.0:

post_install do |installer|
  target = installer.pods_project.targets.find{|t| t.to_s == "MagicalRecord"}
  target.build_configurations.each do |config|
    s = config.build_settings['GCC_PREPROCESSOR_DEFINITIONS']
    s = [ '$(inherited)' ] if s == nil;
    s.push('MR_LOGGING_DISABLED=1') if config.to_s == "Debug";
    config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] = s
  end
end

Changes:

  • project renamed to pods_project
  • Target Pods-MagicalRecord renamed to MagicalRecord
  • Macro MR_ENABLE_ACTIVE_RECORD_LOGGING renamed to MR_LOGGING_DISABLED and value changed from 0 to 1
Ivan Bruel
  • 386
  • 4
  • 10
1

You can switch off logging in Pod project!

Simply add Preprocessor Macros:

  1. Just go into "Pods" (!!!) project.

  2. Then find out Pods-MagicalRecord target.

  3. Choose "Build Settings" tab

  4. Find "Apple LLVM 6.1 Preprocessing" -> "Processor Macros"

  5. Rollout "Processor Macros" and add to "Debug" schema: "MR_ENABLE_ACTIVE_RECORD_LOGGING=0"

It`s all!

0

For the development branch (version 2.3.0 and higher) of Magical Record logging seems to still not work correctly. When imported like this: pod 'MagicalRecord', :git => 'https://github.com/magicalpanda/MagicalRecord', :branch => 'develop'

I have no logging output on my Xcode console. But I altered the post_install script of the Cocoapod. The following should enable logging: https://gist.github.com/Blackjacx/e5f3d62d611ce435775e

With that buildsetting included in GCC_PREPROCESSOR_DEFINITIONS logging of Magical Record can be controlled in 2.3.0++ by using [MagicalRecord setLoggingLevel:]

blackjacx
  • 9,011
  • 7
  • 45
  • 56